<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><div id="myscoll"></div><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Python基础 | XiaoFei🥝</title><meta name="keywords" content="Python基础"><meta name="author" content="XiaoFei🥝"><meta name="copyright" content="XiaoFei🥝"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="Python基础">
<meta property="og:type" content="article">
<meta property="og:title" content="Python基础">
<meta property="og:url" content="https://www.naste.top:1024/posts/99832f47.html">
<meta property="og:site_name" content="XiaoFei🥝">
<meta property="og:description" content="Python基础">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://www.naste.top:9000/webp/2514ce637e374a318653a40133bc66c5.webp">
<meta property="article:published_time" content="2022-06-21T10:01:02.000Z">
<meta property="article:modified_time" content="2024-04-26T09:52:19.207Z">
<meta property="article:author" content="XiaoFei🥝">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://www.naste.top:9000/webp/2514ce637e374a318653a40133bc66c5.webp"><link rel="shortcut icon" href="/"><link rel="canonical" href="https://www.naste.top:1024/posts/99832f47"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.json","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":365,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":230},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdnjs.cloudflare.com/ajax/libs/flickr-justified-gallery/2.1.2/fjGallery.min.js',
      css: 'https://cdnjs.cloudflare.com/ajax/libs/flickr-justified-gallery/2.1.2/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Python基础',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-04-26 17:52:19'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="https://cdn1.tianli0.top/npm/element-ui@2.15.6/packages/theme-chalk/lib/index.css"><style id="themeColor"></style><style id="rightSide"></style><style id="transPercent"></style><style id="blurNum"></style><style id="settingStyle"></style><span id="fps"></span><style id="defineBg"></style><style id="menu_shadow"></style><svg aria-hidden="true" style="position:absolute; overflow:hidden; width:0; height:0"><symbol id="icon-sun" viewBox="0 0 1024 1024"><path d="M960 512l-128 128v192h-192l-128 128-128-128H192v-192l-128-128 128-128V192h192l128-128 128 128h192v192z" fill="#FFD878" p-id="8420"></path><path d="M736 512a224 224 0 1 0-448 0 224 224 0 1 0 448 0z" fill="#FFE4A9" p-id="8421"></path><path d="M512 109.248L626.752 224H800v173.248L914.752 512 800 626.752V800h-173.248L512 914.752 397.248 800H224v-173.248L109.248 512 224 397.248V224h173.248L512 109.248M512 64l-128 128H192v192l-128 128 128 128v192h192l128 128 128-128h192v-192l128-128-128-128V192h-192l-128-128z" fill="#4D5152" p-id="8422"></path><path d="M512 320c105.888 0 192 86.112 192 192s-86.112 192-192 192-192-86.112-192-192 86.112-192 192-192m0-32a224 224 0 1 0 0 448 224 224 0 0 0 0-448z" fill="#4D5152" p-id="8423"></path></symbol><symbol id="icon-moon" viewBox="0 0 1024 1024"><path d="M611.370667 167.082667a445.013333 445.013333 0 0 1-38.4 161.834666 477.824 477.824 0 0 1-244.736 244.394667 445.141333 445.141333 0 0 1-161.109334 38.058667 85.077333 85.077333 0 0 0-65.066666 135.722666A462.08 462.08 0 1 0 747.093333 102.058667a85.077333 85.077333 0 0 0-135.722666 65.024z" fill="#FFB531" p-id="11345"></path><path d="M329.728 274.133333l35.157333-35.157333a21.333333 21.333333 0 1 0-30.165333-30.165333l-35.157333 35.157333-35.114667-35.157333a21.333333 21.333333 0 0 0-30.165333 30.165333l35.114666 35.157333-35.114666 35.157334a21.333333 21.333333 0 1 0 30.165333 30.165333l35.114667-35.157333 35.157333 35.157333a21.333333 21.333333 0 1 0 30.165333-30.165333z" fill="#030835" p-id="11346"></path></symbol></svg><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiperstyle.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-filter-gitcalendar/lib/gitcalendar.css" media="print" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="XiaoFei🥝" type="application/atom+xml">
</head><body><div id="loading-box" onclick="document.getElementById(&quot;loading-box&quot;).classList.add(&quot;loaded&quot;)"><div class="loading-bg"><div class="loading-img"></div><div class="loading-image-dot"></div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://www.naste.top:9000/images/cat.jpg" onerror="onerror=null;src='/assets/r1.jpg'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">149</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">99</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">46</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-home"></use></svg><span class="menu_word" style="font-size:17px"> 首页</span></a></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--article"></use></svg><span class="menu_word" style="font-size:17px"> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-guidang1">                   </use></svg><span class="menu_word" style="font-size:17px"> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-sekuaibiaoqian">                   </use></svg><span class="menu_word" style="font-size:17px"> 标签</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-fenlei">                   </use></svg><span class="menu_word" style="font-size:17px"> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pinweishenghuo"></use></svg><span class="menu_word" style="font-size:17px"> 休闲</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/life/music/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-yinle">                   </use></svg><span class="menu_word" style="font-size:17px"> 八音盒</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/movies/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-dianying1">                   </use></svg><span class="menu_word" style="font-size:17px"> 影院</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/games/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-youxishoubing">                   </use></svg><span class="menu_word" style="font-size:17px"> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xiangzi"></use></svg><span class="menu_word" style="font-size:17px"> 八宝箱</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/box/gallery/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-tubiaozhizuomoban">                   </use></svg><span class="menu_word" style="font-size:17px"> 画廊</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/animation/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-nvwumao">                   </use></svg><span class="menu_word" style="font-size:17px"> 动画</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/nav/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-zhifengche">                   </use></svg><span class="menu_word" style="font-size:17px"> 网址导航</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shejiaoxinxi"></use></svg><span class="menu_word" style="font-size:17px"> 社交</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/social/fcircle/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pengyouquan">                   </use></svg><span class="menu_word" style="font-size:17px"> 朋友圈</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/comments/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-liuyan">                   </use></svg><span class="menu_word" style="font-size:17px"> 留言板</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/social/link/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-lianjie">                   </use></svg><span class="menu_word" style="font-size:17px"> 友人帐</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-wangye"></use></svg><span class="menu_word" style="font-size:17px"> 网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/site/census/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--tongjibiao">                   </use></svg><span class="menu_word" style="font-size:17px"> 网站统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/echarts/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shujutongji1">                   </use></svg><span class="menu_word" style="font-size:17px"> 文章统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/time/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xianxingshalou">                   </use></svg><span class="menu_word" style="font-size:17px"> 旧时光</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-maoliang"></use></svg><span class="menu_word" style="font-size:17px"> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/personal/bb/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-qunliaotian">                   </use></svg><span class="menu_word" style="font-size:17px"> 唠叨</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/love/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-love-sign">                   </use></svg><span class="menu_word" style="font-size:17px"> 恋爱小屋</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/about/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 关于</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/encryption/%E6%9C%BA%E5%9C%BA/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 机场</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">XiaoFei🥝</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-home"></use></svg><span class="menu_word" style="font-size:17px"> 首页</span></a></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--article"></use></svg><span class="menu_word" style="font-size:17px"> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-guidang1">                   </use></svg><span class="menu_word" style="font-size:17px"> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-sekuaibiaoqian">                   </use></svg><span class="menu_word" style="font-size:17px"> 标签</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-fenlei">                   </use></svg><span class="menu_word" style="font-size:17px"> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pinweishenghuo"></use></svg><span class="menu_word" style="font-size:17px"> 休闲</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/life/music/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-yinle">                   </use></svg><span class="menu_word" style="font-size:17px"> 八音盒</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/movies/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-dianying1">                   </use></svg><span class="menu_word" style="font-size:17px"> 影院</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/games/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-youxishoubing">                   </use></svg><span class="menu_word" style="font-size:17px"> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xiangzi"></use></svg><span class="menu_word" style="font-size:17px"> 八宝箱</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/box/gallery/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-tubiaozhizuomoban">                   </use></svg><span class="menu_word" style="font-size:17px"> 画廊</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/animation/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-nvwumao">                   </use></svg><span class="menu_word" style="font-size:17px"> 动画</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/nav/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-zhifengche">                   </use></svg><span class="menu_word" style="font-size:17px"> 网址导航</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shejiaoxinxi"></use></svg><span class="menu_word" style="font-size:17px"> 社交</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/social/fcircle/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pengyouquan">                   </use></svg><span class="menu_word" style="font-size:17px"> 朋友圈</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/comments/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-liuyan">                   </use></svg><span class="menu_word" style="font-size:17px"> 留言板</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/social/link/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-lianjie">                   </use></svg><span class="menu_word" style="font-size:17px"> 友人帐</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-wangye"></use></svg><span class="menu_word" style="font-size:17px"> 网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/site/census/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--tongjibiao">                   </use></svg><span class="menu_word" style="font-size:17px"> 网站统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/echarts/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shujutongji1">                   </use></svg><span class="menu_word" style="font-size:17px"> 文章统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/time/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xianxingshalou">                   </use></svg><span class="menu_word" style="font-size:17px"> 旧时光</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-maoliang"></use></svg><span class="menu_word" style="font-size:17px"> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/personal/bb/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-qunliaotian">                   </use></svg><span class="menu_word" style="font-size:17px"> 唠叨</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/love/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-love-sign">                   </use></svg><span class="menu_word" style="font-size:17px"> 恋爱小屋</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/about/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 关于</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/encryption/%E6%9C%BA%E5%9C%BA/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 机场</span></a></li></ul></div></div><center id="name-container"><a id="page-name" href="javascript:scrollToTop()">PAGE_NAME</a></center><div id="nav-right"><div id="search-button"><a class="search faa-parent animated-hover" title="检索站内任何你想要的信息"><svg class="faa-tada icon" style="height:24px;width:24px;fill:currentColor;position:relative;top:6px" aria-hidden="true"><use xlink:href="#icon-valentine_-search-love-find-heart"></use></svg><span> 搜索</span></a></div><a class="meihua faa-parent animated-hover" onclick="toggleWinbox()" title="美化设置-自定义你的风格" id="meihua-button"><svg class="faa-tada icon" style="height:26px;width:26px;fill:currentColor;position:relative;top:8px" aria-hidden="true"><use xlink:href="#icon-tupian1"></use></svg></a><a class="sun_moon faa-parent animated-hover" onclick="switchNightMode()" title="浅色和深色模式转换" id="nightmode-button"><svg class="faa-tada" style="height:25px;width:25px;fill:currentColor;position:relative;top:7px" viewBox="0 0 1024 1024"><use id="modeicon" xlink:href="#icon-moon">       </use></svg></a><div id="toggle-menu"><a><i class="fas fa-bars fa-fw"></i></a></div></div></div></nav><div id="post-info"><h1 class="post-title">Python基础</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><svg class="meta_icon post-meta-icon" style="width:30px;height:30px;position:relative;top:10px"><use xlink:href="#icon-rili"></use></svg><span class="post-meta-label">发表于 </span><time class="post-meta-date-created" datetime="2022-06-21T10:01:02.000Z" title="发表于 2022-06-21 18:01:02">2022-06-21</time><span class="post-meta-separator">|</span><svg class="meta_icon post-meta-icon" style="width:18px;height:18px;position:relative;top:5px"><use xlink:href="#icon-gengxin1"></use></svg><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-04-26T09:52:19.207Z" title="更新于 2024-04-26 17:52:19">2024-04-26</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><svg class="meta_icon post-meta-icon" style="width:18px;height:18px;position:relative;top:5px"><use xlink:href="#icon-biaoqian"></use></svg><a class="post-meta-categories" href="/categories/Python/">Python</a><i class="fas fa-angle-right post-meta-separator"></i><svg class="meta_icon post-meta-icon" style="width:18px;height:18px;position:relative;top:5px"><use xlink:href="#icon-biaoqian"></use></svg><a class="post-meta-categories" href="/categories/Python/Python%E4%BD%BF%E7%94%A8/">Python使用</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><svg class="meta_icon post-meta-icon" style="width:25px;height:25px;position:relative;top:8px"><use xlink:href="#icon-charuword"></use></svg><span class="post-meta-label">字数总计:</span><span class="word-count">3w</span><span class="post-meta-separator">|</span><svg class="meta_icon post-meta-icon" style="width:20px;height:20px;position:relative;top:5px"><use xlink:href="#icon-shizhong"></use></svg><span class="post-meta-label">阅读时长:</span><span>104分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Python基础"><svg class="meta_icon post-meta-icon" style="width:25px;height:25px;position:relative;top:5px"><use xlink:href="#icon-eye"></use></svg><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><section class="main-hero-waves-area waves-area"><svg class="waves-svg" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto"><defs><path id="gentle-wave" d="M -160 44 c 30 0 58 -18 88 -18 s 58 18 88 18 s 58 -18 88 -18 s 58 18 88 18 v 44 h -352 Z"></path></defs><g class="parallax"><use href="#gentle-wave" x="48" y="0"></use><use href="#gentle-wave" x="48" y="3"></use><use href="#gentle-wave" x="48" y="5"></use><use href="#gentle-wave" x="48" y="7"></use></g></svg></section></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="一、Python入门"><a href="#一、Python入门" class="headerlink" title="一、Python入门"></a>一、Python入门</h1><h2 id="1、Python基本概念"><a href="#1、Python基本概念" class="headerlink" title="1、Python基本概念"></a>1、Python基本概念</h2><ol>
<li>表达式<ul>
<li>表达式就是一个类似于数学公式的东西</li>
<li>比如：10 + 5   8 - 4</li>
<li>表达式一般仅仅用了计算一些结果，不会对程序产生实质性的影响</li>
<li>如果在交互模式中输入一个表达式，解释器会自动将表达式的结果输出</li>
</ul>
</li>
<li>语句<ul>
<li>在程序中语句一般需要完成某种功能，比如打印信息、获取信息、为变量赋值。。。</li>
<li>比如：<ul>
<li>print()       </li>
<li>input()        </li>
<li>a = 10</li>
</ul>
</li>
<li>在交互模式中不一定会输出语句的执行结果  </li>
</ul>
</li>
<li>程序（program）<ul>
<li>程序就是由一条一条的语句和一条一条的表达式构成的。</li>
</ul>
</li>
<li>函数（function）<ul>
<li>函数就是一种语句，函数专门用来完成特定的功能 </li>
<li>函数长的形如：xxx()   </li>
<li>函数的分类：<ul>
<li>内置函数：<ul>
<li>由Python解释器提供的函数，可以在Python中直接使用</li>
</ul>
</li>
<li>自定义函数：<ul>
<li>由程序员自主的创建的函数</li>
</ul>
</li>
</ul>
</li>
<li>当我们需要完成某个功能时，就可以去调用内置函数，或者自定义函数</li>
<li>函数的两个要素：<ul>
<li>参数：<ul>
<li>()中的内容就是函数的参数</li>
<li>函数中可以没有参数，也可以有多个参数，多个参数之间使用,隔开</li>
</ul>
</li>
<li>返回值：- 返回值是函数的返回结果，不是所有的函数都有返回值</li>
</ul>
</li>
</ul>
</li>
</ol>
<h2 id="2、基本语法"><a href="#2、基本语法" class="headerlink" title="2、基本语法"></a>2、基本语法</h2><ul>
<li><p>在Python中严格区分大小写</p>
</li>
<li><p>Python中的每一行就是一条语句，每条语句以换行结束，语句后面不需要加 ; 结束，换行就结束了</p>
</li>
<li><p>Python中每一行语句不要过长（规范中建议每行不要超过80个字符）    </p>
<ul>
<li>“rulers”:[80],</li>
</ul>
</li>
<li><p>一条语句可以分多行编写，<strong>多行编写时语句后边以\结尾</strong> </p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#一条赋值语句多行编写，\就像Java中的 + 一样</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">Java中一条语句多行写</span></span><br><span class="line"><span class="string">String str=&quot;how &quot; + </span></span><br><span class="line"><span class="string">&quot;are &quot; +</span></span><br><span class="line"><span class="string">&quot;you&quot;</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line">a: <span class="built_in">str</span> = <span class="string">&quot;how &quot;</span> \</span><br><span class="line">         <span class="string">&quot;are &quot;</span> \</span><br><span class="line">         <span class="string">&quot;you&quot;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>Python是缩进严格的语言，所以在Python中不要随便写缩进</p>
</li>
<li><p>在Python中使用【 # 、’’’  ‘’’ 、”””  这是文档注释  “””】来表示注释，#后的内容都属于注释，注释的内容将会被解释器所忽略</p>
<ul>
<li><p>我们可以通过注释来对程序进行解释说明，一定要养成良好的编写注释的习惯</p>
</li>
<li><p>注释要求简单明了，一般习惯上#后边会跟着一个空格</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;&quot; </span></span><br><span class="line"><span class="string">我是多行注释 </span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">我是多行注释</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#我是单行注释</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h2 id="3、字面量和变量"><a href="#3、字面量和变量" class="headerlink" title="3、字面量和变量"></a>3、字面量和变量</h2><ul>
<li>字面量就是一个一个的值，比如：1，2，3，4，5，6，‘HELLO’<ul>
<li>字面量所表示的意思就是它的字面的值，在程序中可以直接使用字面量</li>
</ul>
</li>
<li>变量（variable）变量可以用来保存字面量，并且变量中保存的字面量是不定的<ul>
<li>变量本身没有任何意思，它会根据不同的字面量表示不同的意思</li>
</ul>
</li>
<li>一般我们在开发时，很少直接使用字面量，都是将字面量保存到变量中，通过变量来引用字面量</li>
</ul>
<h2 id="4、变量和标识符"><a href="#4、变量和标识符" class="headerlink" title="4、变量和标识符"></a>4、变量和标识符</h2><ul>
<li><p>变量</p>
<ul>
<li>Python中使用变量，不需要声明，直接为变量赋值即可（如：a=10）</li>
<li>不能使用没有进行过赋值的变量</li>
<li>如果使用没有赋值过的变量，会报错：NameError: name ‘a’ is not defined（变量a没有定义）</li>
<li>Python是一个动态类型的语言，可以为变量赋任意类型的值，也可以任意修改变量的值</li>
</ul>
</li>
<li><p>标识符</p>
<ul>
<li>在Python中所有可以自主命名的内容都属于标识符</li>
<li>比如：变量名、函数名、类名</li>
<li>标识符必须遵循标识符的规范<ul>
<li>标识符中可以含有字母、数字、_，但是不能使用数字开头<ul>
<li>例如：例子：a_1 _a1 _1a</li>
</ul>
</li>
<li>标识符不能是Python中的关键字和保留字<ul>
<li>也不建议使用Python中的函数名作为标识符,因为这样会导致函数被覆盖</li>
</ul>
</li>
<li>命名规范：<ul>
<li>在Python中注意遵循两种命名规范：<ul>
<li>下划线命名法<ul>
<li>所有字母小写，单词之间使用_分割</li>
<li>max_length min_length hello_world xxx_yyy_zzz</li>
</ul>
</li>
<li>帕斯卡命名法（大驼峰命名法）  <ul>
<li>首字母大写，每个单词开头字母大写，其余字母小写</li>
<li>MaxLength MinLength HelloWorld XxxYyyZzz  </li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>如果使用不符合标准的标识符，将会报错 SyntaxError: invalid syntax</li>
</ul>
</li>
</ul>
<h2 id="5、数据类型"><a href="#5、数据类型" class="headerlink" title="5、数据类型"></a>5、数据类型</h2><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">数据类型指的就是变量的值得类型，也就是可以为变量赋哪些值 </span><br><span class="line">数值</span><br><span class="line">    整型</span><br><span class="line">        布尔值</span><br><span class="line">    浮点型</span><br><span class="line">    复数</span><br><span class="line">字符串</span><br><span class="line">空值</span><br></pre></td></tr></table></figure>
<ul>
<li><p>在Python数值分成了三种：<strong>整数、浮点数（小数）、复数</strong></p>
</li>
<li><p>在Python中<strong>所有的整数都是int类型</strong></p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">20</span></span><br><span class="line">b = <span class="number">10</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>Python中的<strong>整数的大小没有限制</strong>，可以是一个无限大的整数</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c = <span class="number">999999999999999999999999999999999999999999999</span> ** <span class="number">100</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>如果数字的长度过大，可以使用下划线作为分隔符</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c = <span class="number">123_456_789</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>d = 0123、 <strong>10进制的数字不能以0开头</strong></p>
</li>
<li><p>其他进制的整数，只要是<strong>数字打印时一定是以十进制的形式显示的</strong></p>
</li>
<li><p>二进制 0b开头</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c = <span class="number">0b10</span> <span class="comment"># 二进制的10</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>八进制 0o开头</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c = <span class="number">0o10</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>十六进制 0x开头</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c = <span class="number">0x10</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>也可以通过运算符来对数字进行运算，并且可以保证整数运算的精确</p>
</li>
<li><p>浮点数（小数），在Python中<strong>所有的小数都是float类型</strong></p>
</li>
<li><p>对浮点数进行运算时，可能会得到一个不精确的结果</p>
</li>
</ul>
<h2 id="6、字符串-str"><a href="#6、字符串-str" class="headerlink" title="6、字符串(str)"></a>6、字符串(str)</h2><ul>
<li><p>字符串用来表示一段文本信息，字符串是程序中使用的最多的数据类型</p>
</li>
<li><p>在Python中字符串需要使用引号引起来，如果不引起来的话，会被当成是一个变量、例如：</p>
<ul>
<li>引号可以是双引号，也可以是单引号，但是注意不要混着用（不能一个单引号和一个双引号，’  “ ）</li>
<li><p>单引号里面可以使用双引号，双引号里面可以使用单引号，单双引号不会跨行找引号</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#此时系统会以为在abc为一个变量，而此时还没有定义abc，所以就会报异常</span></span><br><span class="line">a=abc</span><br><span class="line"><span class="comment">#需要使用如下方式定义,使用单引号或双引号都可以给变量赋值为字符串</span></span><br><span class="line">a=<span class="string">&#x27;abc&#x27;</span> 或 a=<span class="string">&quot;abc&quot;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>对于长字符串</p>
<ul>
<li><p>长字符串</p>
</li>
<li><p>单引号和双引号不能跨行使用</p>
</li>
<li><p>使用三重引号来表示一个长字符串 ‘’’ “””</p>
</li>
<li><p>三重引号可以换行(单双都可以)，并且会保留字符串中的格式</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">s = <span class="string">&#x27;&#x27;&#x27;锄禾日当午，</span></span><br><span class="line"><span class="string">汗滴禾下土，</span></span><br><span class="line"><span class="string">谁知盘中餐，</span></span><br><span class="line"><span class="string">粒粒皆辛苦&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>转义字符</p>
<ul>
<li><p>可以使用 \ 作为转义字符，通过转义字符，可以在字符串中使用一些特殊的内容</p>
</li>
<li><p>例子：</p>
<ul>
<li><p>\ ‘ 表示’</p>
</li>
<li><p>\ “ 表示”</p>
</li>
<li><p>\t 表示制表符</p>
<p>  \n 表示换行符</p>
</li>
<li><p>\ \ 表示反斜杠</p>
</li>
<li><p>\uxxxx 表示Unicode编码</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="7、格式化字符串"><a href="#7、格式化字符串" class="headerlink" title="7、格式化字符串"></a>7、格式化字符串</h2><ul>
<li><p>字符串之间也可以进行加法运算</p>
</li>
<li><p>如果将两个字符串进行相加，则会自动将两个字符串拼接为一个</p>
<ul>
<li><p>字符串相加使用方法</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="string">&#x27;abc&#x27;</span> + <span class="string">&#x27;haha&#x27;</span> + <span class="string">&#x27;哈哈&#x27;</span></span><br><span class="line"><span class="built_in">print</span>(a) <span class="comment">#输出结果为：abchaha哈哈</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>字符串不能和其他的类型进行加法运算，如果做了会出现异常 TypeError: must be str, not int</p>
<ul>
<li><p>+号连接字符串使用方法，如下代码会报异常：TypeError: can only concatenate str (not “int”) to str</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">123</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a=&quot;</span> + a) <span class="comment">#此方法不推荐使用</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a=&quot;</span> , a) <span class="comment">#此方法，每个逗号隔开的东西，最后输出的时候会有空格隔开的</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>在创建字符串时，可以在字符串中指定占位符，可以一次性填充多个占位符</p>
<ul>
<li><p>%s ：在字符串中表示任意类型字符，（int ，float 、等字符串都可以填充）</p>
</li>
<li><p>%f ： 浮点数占位符( 保留位数使用：%（.x）f ，（）中的数值为几，就保留几位小数，此时会使用四舍五入 )</p>
</li>
<li><p>%d：整数占位符，输入小数的话，会把小数后面的全部舍去，不会进行四舍五入</p>
</li>
<li><p>占位符使用方法：</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">b = <span class="string">&#x27;Hello %s&#x27;</span> % <span class="string">&#x27;孙悟空&#x27;</span></span><br><span class="line">c = <span class="string">&#x27;hello %s 你好 %s&#x27;</span> % (<span class="string">&#x27;tom&#x27;</span>, <span class="string">&#x27;孙悟空&#x27;</span>)</span><br><span class="line">d = <span class="string">&#x27;hello %3.5s&#x27;</span> % <span class="string">&#x27;abcdefg&#x27;</span>  <span class="comment"># %3.5s字符串的长度限制在3-5之间</span></span><br><span class="line">e = <span class="string">&#x27;hello %s 你好 %s&#x27;</span> % (<span class="number">123.456</span>, <span class="number">24</span>)</span><br><span class="line">f = <span class="string">&#x27;hello %f&#x27;</span> % <span class="number">123</span></span><br><span class="line">g = <span class="string">&#x27;hello %d&#x27;</span> % -<span class="number">123.95</span></span><br><span class="line">h = <span class="string">&#x27;呵呵&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;h=%s&#x27;</span> % b) <span class="comment">#输出：h=Hello 孙悟空</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>格式化字符串，可以通过在字符串前添加一个f来创建一个格式化字符串</p>
</li>
<li><p>在格式化字符串中可以直接嵌入变量 </p>
<ul>
<li><p>接嵌入变量 使用方法</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#&#123; 此处可以放入变量 &#125;，注意，放在&#123;&#125;中的变量必须是有效的，否则会报错</span></span><br><span class="line">a = <span class="string">&#x27;how&#x27;</span>	</span><br><span class="line">b = <span class="string">&#x27;are&#x27;</span>	</span><br><span class="line">c = <span class="string">&#x27;you&#x27;</span>	</span><br><span class="line">d = <span class="string">f&#x27;hello ：<span class="subst">&#123;a&#125;</span> <span class="subst">&#123;b&#125;</span> <span class="subst">&#123;c&#125;</span>&#x27;</span>	<span class="comment">#先输出，赋给一个变量</span></span><br><span class="line"><span class="built_in">print</span>(d)	<span class="comment">#输出：hello ：how are you</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&#x27;a=<span class="subst">&#123;a&#125;</span> <span class="subst">&#123;b&#125;</span> <span class="subst">&#123;c&#125;</span>&#x27;</span>)	<span class="comment">#直接输出，输出结果：a=how are you</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>练习</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">name = <span class="string">&quot;孙悟空&quot;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;欢迎 &quot;</span> + name + <span class="string">&quot; 光临&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;欢迎&quot;</span>, name, <span class="string">&quot;光临&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;欢迎 %s 光临&quot;</span> % name)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&#x27;欢迎 <span class="subst">&#123;name&#125;</span> 光临&#x27;</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="8、字符串复制"><a href="#8、字符串复制" class="headerlink" title="8、字符串复制"></a>8、字符串复制</h2><ul>
<li><p>将字符串和数字相乘</p>
</li>
<li><p>（ * ）在语言中表示乘法</p>
</li>
<li><p>如果将字符串和数字相乘，则解释器会将字符串重复指定的次数并返回</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="string">&#x27;abc&#x27;</span></span><br><span class="line"><span class="built_in">print</span>(a * <span class="number">2</span>) <span class="comment">#输出结果为：abcabc</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="9、布尔值和空值"><a href="#9、布尔值和空值" class="headerlink" title="9、布尔值和空值"></a>9、布尔值和空值</h2><ul>
<li><p>空值：None</p>
<ul>
<li>None专门用来表示不存在</li>
</ul>
</li>
<li><p>布尔值主要用来做逻辑判断</p>
<ul>
<li><p>布尔值一共有两个 True 和 False，True表示真 False表示假</p>
</li>
<li><p>布尔值实际上也属于整型，True就相当于1，False就相当于0</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="literal">True</span>  <span class="comment"># 相当于1</span></span><br><span class="line">b = <span class="literal">False</span>  <span class="comment"># 相当于0</span></span><br><span class="line"><span class="built_in">print</span>(a + <span class="number">1</span>)  <span class="comment"># 相当于1+1，结果为：2</span></span><br><span class="line"><span class="built_in">print</span>(b - <span class="number">1</span>)  <span class="comment"># 相当于0-1，结果为：-1</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h2 id="10、类型检查"><a href="#10、类型检查" class="headerlink" title="10、类型检查"></a>10、类型检查</h2><ul>
<li><p>通过类型检查，可以检查只能值（变量）的类型</p>
</li>
<li><p>type()用来检查值的类型</p>
</li>
<li><p>该函数会将检查的结果作为返回值返回，可以通过变量来接收函数的返回值</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">123</span></span><br><span class="line">c = <span class="built_in">type</span>(<span class="string">&#x27;123&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(c))  <span class="comment"># &lt;class &#x27;type&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line">c = <span class="built_in">type</span>(a)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(c))  <span class="comment"># &lt;class &#x27;type&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(<span class="number">1</span>))  <span class="comment"># &lt;class &#x27;int&#x27;&gt;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(<span class="number">1.5</span>))  <span class="comment"># &lt;class &#x27;float&#x27;&gt;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(<span class="literal">True</span>))  <span class="comment"># &lt;class &#x27;bool&#x27;&gt;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(<span class="string">&#x27;hello&#x27;</span>))  <span class="comment"># &lt;class &#x27;str&#x27;&gt;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(<span class="literal">None</span>))  <span class="comment"># &lt;class &#x27;NoneType&#x27;&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="11、对象"><a href="#11、对象" class="headerlink" title="11、对象"></a>11、对象</h2><ul>
<li><p>Python是一门面向对象的语言，一切皆对象！</p>
</li>
<li><p>程序运行当中，所有的数据都是存储到内存当中然后再运行的！</p>
</li>
<li><p>对象就是内存中专门用来存储指定数据的一块区域</p>
</li>
<li><p>对象实际上就是一个容器，专门用来存储数据</p>
</li>
<li><p>像我们之前学习的数值、字符串、布尔值、None都是对象</p>
</li>
<li><p>参考 下图：</p>
<p>  <img src= "" data-lazy-src="https://www.naste.top:9000/webp/image-20210309214156533.webp" alt="image-20210309214156533"></p>
</li>
</ul>
<h2 id="12、对象的结构"><a href="#12、对象的结构" class="headerlink" title="12、对象的结构"></a>12、对象的结构</h2><ul>
<li><p>每个对象中都要保存三种数据</p>
<ul>
<li><p>id（标识）</p>
<ul>
<li><p>id用来标识对象的唯一性，每一个对象都有唯一的id</p>
</li>
<li><p>对象的id就相当于人的身份证号一样</p>
</li>
<li><p>可以通过id()函数来查看对象的id。</p>
</li>
<li><p>从下面测试可以看出对于相同的数据的id是一样的，不同数据的id是不一样的</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">1</span></span><br><span class="line">b = <span class="number">2</span></span><br><span class="line">c = <span class="number">1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(a))<span class="comment">#140703480809120</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(b))<span class="comment">#140703480809152</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(c))<span class="comment">#140703480809120</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(a) == <span class="built_in">id</span>(b))<span class="comment">#False</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<pre><code>    - id是由解析器生成的，在CPython中，id就是对象的内存地址

    - 对象一旦创建，则它的id永远不能再改变

- type（类型）

    - 类型用来标识当前对象所属的类型
    - 比如：int str float bool 。。。
    - 类型决定了对象有哪些功能
    - 通过type()函数来查看对象的类型
    - Python是一门强类型的语言，对象一旦创建类型便不能修改

- value（值）

    - 值就是对象中存储的具体的数据
    - 对于有些对象值是可以改变的
    - 对象分成两大类，可变对象 不可变对象
        - 可变对象的值可以改变
        - 不可变对象的值不能改变，之前学习的对象都是不可变对象

- 参考下图：

    ![image-20210309215018157](https://www.naste.top:9000/webp/image-20210309215018157.webp)
</code></pre><h2 id="13、变量和对象"><a href="#13、变量和对象" class="headerlink" title="13、变量和对象"></a>13、变量和对象</h2><ul>
<li>对象并没有直接存储到变量中，在Python中变量更像是给对象起了一个别名</li>
<li>变量中存储的不是对象的值，而是对象的id（内存地址），<ul>
<li>当我们使用变量时，实际上就是在通过对象id在查找对象</li>
</ul>
</li>
<li>变量中保存的对象，只有在为变量重新赋值时才会改变</li>
<li>变量和变量之间是相互独立的，修改一个变量不会影响另一个变量</li>
<li>参考下图<ul>
<li><img src= "" data-lazy-src="https://www.naste.top:9000/webp/image-20210310123840728.webp" alt="image-20210310123840728"></li>
</ul>
</li>
</ul>
<h2 id="14、类型转换"><a href="#14、类型转换" class="headerlink" title="14、类型转换"></a>14、类型转换</h2><ul>
<li>类型转换实际是创建了一个新的对象，而不是修改之前的对象。Python是一门强类型的语言，对象一旦创建类型便不能修改</li>
<li>所谓的类型转换，将一个类型的对象转换为其他对象</li>
<li><strong>类型转换不是改变对象本身的类型</strong>，而是根据当前对象的值创建一个新对象</li>
<li>类型转换四个函数 int() float() str() bool()</li>
<li>规则：<ul>
<li>对于其他不可转换为整型的对象，直接抛出异常 ValueError</li>
<li>str() 可以将对象转换为字符串</li>
<li>bool() 可以将对象转换为布尔值，bool值可以转换为任何对象，任何对象都可以转换为布尔值<ul>
<li>布尔值：True -&gt; 1   False -&gt; 0</li>
<li>对于所有表示空性的对象都会转换为False，其余的转换为True</li>
<li>哪些表示的空性：0 、 None 、 ‘’ 。。。</li>
</ul>
</li>
<li>int()函数不会对原来的变量产生影响，他是对象转换为指定的类型并将其作为返回值返回<ul>
<li>浮点数：直接取整，省略小数点后的内容</li>
<li>字符串：合法的整数字符串，直接转换为对应的数字</li>
<li>如果不是一个合法的整数字符串，则报错 ValueError: invalid literal for int() with base 10: ‘11.5’</li>
</ul>
</li>
<li>float() 和 int()基本一致，不同的是它会将对象转换为浮点数</li>
<li>调用如上函数不会修改原来的变量，如果希望修改原来的变量，则需要对变量进行重新赋值</li>
</ul>
</li>
</ul>
<h2 id="15、运算符-操作符"><a href="#15、运算符-操作符" class="headerlink" title="15、运算符(操作符)"></a>15、运算符(操作符)</h2><ul>
<li>运算符可以对一个值或多个值进行运算或各种操作</li>
<li><p>比如 + 、-、= 都属于运算符</p>
</li>
<li><p>运算符的分类：</p>
<ul>
<li><p>1.算术运算符</p>
<ul>
<li><p>(+) 加法运算符、- 减法运算符、* 乘法运算符、/ 除法运算符</p>
</li>
<li><p>// 整除，只会保留计算后的整数位，总会返回一个整型</p>
</li>
<li><p>** 幂运算，求一个值的几次幂</p>
</li>
<li><p>% 取模，求两个数相除的余数</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">c = <span class="number">25</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;c=&#x27;</span>, c // <span class="number">2</span>)  <span class="comment"># 输出：12</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;c=&#x27;</span>, c ** <span class="number">2</span>)  <span class="comment"># 输出：625</span></span><br><span class="line">c = <span class="number">4</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;c=&#x27;</span>, c ** <span class="number">0.5</span>)  <span class="comment"># 输出:2.0</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<pre><code>- 2.赋值运算符

- 3.比较运算符（关系运算符）

- 4.逻辑运算符

- 5.条件运算符（三元运算符）    
</code></pre><h2 id="16、赋值运算符"><a href="#16、赋值运算符" class="headerlink" title="16、赋值运算符"></a>16、赋值运算符</h2><ul>
<li>赋值运算符</li>
<li>在对浮点数做算术运算时，结果也会返回一个浮点数<ul>
<li>= 可以将等号右侧的值赋值给等号左侧的变量</li>
<li>+=  a += 5 相当于 a = a + 5 </li>
<li>-=  a -= 5 相当于 a = a - 5 </li>
<li><em>=  a </em>= 5 相当于 a = a * 5 </li>
<li>**= a = 5 相当于 a = a  5 </li>
<li>/=  a /= 5 相当于 a = a / 5 </li>
<li>//= a //= 5 相当于 a = a // 5 </li>
<li>%=  a %= 5 相当于 a = a % 5 </li>
</ul>
</li>
</ul>
<h2 id="17、关系运算符"><a href="#17、关系运算符" class="headerlink" title="17、关系运算符"></a>17、关系运算符</h2><ul>
<li><p>关系运算符用来比较两个值之间的关系，总会返回一个布尔值</p>
</li>
<li><p>关系运算符有：&gt;、&gt;=、&lt;、&lt;=、==、!=</p>
</li>
<li><p>is 比较两个对象是否是同一个对象，比较的是对象的id</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#使用方式：</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">1</span> <span class="keyword">is</span> <span class="number">1</span>) <span class="comment">#输出：True</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">1</span> <span class="keyword">is</span> <span class="number">2</span>) <span class="comment">#输出：False</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>is not 比较两个对象是否不是同一个对象，比较的是对象的id</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#使用方式：</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">1</span> <span class="keyword">is</span> <span class="number">1</span>) <span class="comment">#输出：False</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">1</span> <span class="keyword">is</span> <span class="number">2</span>) <span class="comment">#输出：True</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在Python中可以对两个字符串进行大于（等于）或小于（等于）的运算</p>
<ul>
<li>当对字符串进行比较时，实际上比较的是字符串的Unicode编码</li>
<li>比较两个字符串的Unicode编码时，是逐位比较的</li>
<li>利用该特性可以对字符串按照字母顺序进行排序，但是对于中文来说意义不是特别大</li>
<li>注意：如果不希望比较两个字符串的Unicode编码，则需要将其转换为数字然后再比较</li>
</ul>
</li>
<li><p>逻辑运算符可以连着使用（全是和中间数字比较）</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">result = <span class="number">1</span> &lt; <span class="number">2</span> &lt; <span class="number">3</span> <span class="comment"># 相当于 1 &lt; 2 and 2 &lt; 3</span></span><br><span class="line">result = <span class="number">10</span> &lt; <span class="number">20</span> &gt; <span class="number">15</span> <span class="comment"># 相当于 20 &gt;10 且 20 &gt; 15</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="18、逻辑运算符"><a href="#18、逻辑运算符" class="headerlink" title="18、逻辑运算符"></a>18、逻辑运算符</h2><ul>
<li><p>逻辑运算符主要用来做一些逻辑判断</p>
</li>
<li><p>not 逻辑非</p>
<ul>
<li><p>not可以对符号右侧的值进行非运算</p>
<ul>
<li><p>对于布尔值，非运算会对其进行取反操作，True变False，False变True</p>
</li>
<li><p>对于非布尔值，非运算会先将其转换为布尔值，然后再取反</p>
<ul>
<li><p>哪些表示的空性：（0 、 None 、’’）（这些转换为bool类型的值为False，其他的全部转换为True）</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="built_in">bool</span>(<span class="number">1</span>)) <span class="comment">#True</span></span><br><span class="line"><span class="built_in">print</span>(<span class="keyword">not</span> <span class="number">1</span>) <span class="comment">#False</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>and 逻辑与</p>
<ul>
<li>and可以对符号两侧的值进行与运算</li>
<li>只有在符号两侧的值都为True时，才会返回True，<strong>只要有一个为False就返回False</strong></li>
<li>与运算是找False的</li>
<li>Python中的与运算是短路的与，如果第一个值为False，则不再看第二个值</li>
</ul>
</li>
<li><p>or 逻辑或</p>
<ul>
<li>or 可以对符号两侧的值进行或运算</li>
<li>或运算两个值中<strong>只要有一个True，就会返回True</strong></li>
<li>或运算是找True的</li>
<li>Python中的或运算是短路的或，如果第一个值为True，则不再看第二个值</li>
</ul>
</li>
<li><p>使用：</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">True</span> <span class="keyword">and</span> <span class="built_in">print</span>(<span class="string">&#x27;你猜我出来吗？&#x27;</span>) <span class="comment">#第一个值是True，会看第二个值，所以print()会执行</span></span><br><span class="line"><span class="literal">False</span> <span class="keyword">and</span> <span class="built_in">print</span>(<span class="string">&#x27;你猜我出来吗？&#x27;</span>) <span class="comment">#第一个值是False，不会看第二个值，所以print()不会执行</span></span><br></pre></td></tr></table></figure>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">False</span> <span class="keyword">or</span> <span class="built_in">print</span>(<span class="string">&#x27;你猜我出来吗？&#x27;</span>) <span class="comment">#第一个值为False，继续看第二个，所以打印语句执行</span></span><br><span class="line"><span class="literal">True</span> <span class="keyword">or</span> <span class="built_in">print</span>(<span class="string">&#x27;你猜我出来吗？&#x27;</span>) <span class="comment">#第一个值为True，不看第二个，所以打印语句不执行</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>非布尔值的与（and）或（or）运算</p>
<ul>
<li><p>当我们对非布尔值进行与或运算时，Python会将其当做布尔值运算，最终会返回原值</p>
</li>
<li><p>与（and）运算的规则</p>
<ul>
<li><p>与运算是找False的，如果第一个值是False，则不看第二个值</p>
</li>
<li><p>如果第一个值是False，则直接返回第一个值，否则返回第二个值</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># True and True</span></span><br><span class="line">result = <span class="number">1</span> <span class="keyword">and</span> <span class="number">2</span> <span class="comment"># 2</span></span><br><span class="line"><span class="comment"># True and False</span></span><br><span class="line">result = <span class="number">1</span> <span class="keyword">and</span> <span class="number">0</span> <span class="comment"># 0</span></span><br><span class="line"><span class="comment"># False and True</span></span><br><span class="line">result = <span class="number">0</span> <span class="keyword">and</span> <span class="number">1</span> <span class="comment"># 0</span></span><br><span class="line"><span class="comment"># False and False</span></span><br><span class="line">result = <span class="number">0</span> <span class="keyword">and</span> <span class="literal">None</span> <span class="comment"># 0</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<pre><code>- 或（or）运算的规则

    - 或运算是找True的，如果第一个值是True，则不看第二个值

    - 如果第一个值是True，则直接返回第一个值，否则返回第二个值   

        <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># True or True</span></span><br><span class="line">result = <span class="number">1</span> <span class="keyword">or</span> <span class="number">2</span> <span class="comment"># 1</span></span><br><span class="line"><span class="comment"># True or False</span></span><br><span class="line">result = <span class="number">1</span> <span class="keyword">or</span> <span class="number">0</span> <span class="comment"># 1</span></span><br><span class="line"><span class="comment"># False or True</span></span><br><span class="line">result = <span class="number">0</span> <span class="keyword">or</span> <span class="number">1</span> <span class="comment"># 1</span></span><br><span class="line"><span class="comment"># False or False</span></span><br><span class="line">result = <span class="number">0</span> <span class="keyword">or</span> <span class="literal">None</span> <span class="comment"># None</span></span><br></pre></td></tr></table></figure>
</code></pre><h2 id="19、条件运算符（三元运算符）"><a href="#19、条件运算符（三元运算符）" class="headerlink" title="19、条件运算符（三元运算符）"></a>19、条件运算符（三元运算符）</h2><ul>
<li><p>语法： 语句1 if 条件表达式 else 语句2</p>
</li>
<li><p>执行流程：</p>
<ul>
<li><p>条件运算符在执行时，会先对条件表达式进行求值判断</p>
<ul>
<li><p>如果判断结果为True，则执行语句1，并返回执行结果</p>
</li>
<li><p>如果判断结果为False，则执行语句2，并返回执行结果</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">1</span></span><br><span class="line">b = <span class="number">2</span></span><br><span class="line">c = <span class="number">3</span></span><br><span class="line"><span class="comment"># 输出：最大值为c</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;最大值为a&#x27;</span>) <span class="keyword">if</span> a &gt; b <span class="keyword">else</span> (<span class="built_in">print</span>(<span class="string">&#x27;最大值为b&#x27;</span>) <span class="keyword">if</span> b &gt; c <span class="keyword">else</span> <span class="built_in">print</span>(<span class="string">&#x27;最大值为c&#x27;</span>))</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="20、运算符的优先级"><a href="#20、运算符的优先级" class="headerlink" title="20、运算符的优先级"></a>20、运算符的优先级</h2><ul>
<li><p>和数学中一样，在Python运算也有优先级，比如先乘除 后加减</p>
</li>
<li><p>运算符的优先级可以根据优先级的表格来查询，在表格中位置越靠下的运算符优先级越高，优先级越高的越优先计算，如果优先级一样则自左向右计算</p>
</li>
<li><p>在开发中如果遇到优先级不清楚的,则可以通过小括号来改变运算顺序</p>
</li>
<li><p>逻辑运算符可以连着使用（全是和中间数字比较）</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">result = <span class="number">1</span> &lt; <span class="number">2</span> &lt; <span class="number">3</span> <span class="comment"># 相当于 1 &lt; 2 and 2 &lt; 3</span></span><br><span class="line">result = <span class="number">10</span> &lt; <span class="number">20</span> &gt; <span class="number">15</span> <span class="comment"># 相当于 20 &gt;10 且 20 &gt; 15</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="二、流程控制语句"><a href="#二、流程控制语句" class="headerlink" title="二、流程控制语句"></a>二、流程控制语句</h1><h2 id="1、条件判断语句"><a href="#1、条件判断语句" class="headerlink" title="1、条件判断语句"></a>1、条件判断语句</h2><ul>
<li><p>语法：</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件表达式:</span><br><span class="line">    代码块</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>代码块</p>
<ul>
<li>代码块中保存着一组代码，同一个代码块中的代码，要么都执行要么都不执行</li>
<li>代码块就是一种为代码分组的机制</li>
<li>如果要编写代码块，语句就不能紧随在:后边，而是要写在下一行</li>
<li>代码块以缩进开始，直到代码恢复到之前的缩进级别时结束</li>
</ul>
</li>
<li><p>if只能执行一条语句和执行多条语句的格式：</p>
<ul>
<li><p>只能执行一条语句：将满足条件需要执行的语句放在表达式的后面，此时只会执行一条语句</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> <span class="literal">False</span>: <span class="built_in">print</span>(<span class="number">10</span>) <span class="comment">#此时由于if条件不满足，此处不会执行</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;我不受if语句的控制&quot;</span>) <span class="comment">#次数会执行，输出为：我不受if语句的控制</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>执行多条语句，语句不能在和if在同一行，且需要执行语句需要缩进，</p>
</li>
<li><p>缩进有两种方式，一种是使用tab键，一种是使用空格（四个）</p>
<ul>
<li>Python的官方文档中推荐我们使用空格来缩进</li>
<li>Python代码中使用的缩进方式必须统一</li>
</ul>
</li>
<li><p>执行多条语句的时候，恢复到之前的缩进就可以退出if的判断</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">num = <span class="number">10</span></span><br><span class="line"><span class="keyword">if</span> num &gt; <span class="number">10</span>:</span><br><span class="line">    <span class="comment">#如下两行代码都会受到if条件的限制</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;我受if语句的控制&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;我受if语句的控制2&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;我不受if语句的控制&quot;</span>) <span class="comment">#此处由于没有缩进，所以不受if语句的限制</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>执行的流程：if语句在执行时，会先对条件表达式进行求值判断，</p>
</li>
<li><p>可以使用逻辑运算符来连接多个条件，</p>
<ul>
<li>如果希望所有条件同时满足，则需要使用and</li>
<li>如果希望只要有一个条件满足即可，则需要使用or</li>
</ul>
</li>
</ul>
<h2 id="2、input函数"><a href="#2、input函数" class="headerlink" title="2、input函数"></a>2、input函数</h2><ul>
<li><p>该函数用来获取用户的输入，输入的东西为str类型的</p>
</li>
<li><p>input()调用后，程序会立即暂停，等待用户输入，用户输入完内容以后，点击回车程序才会继续向下执行，用户输入完成以后，其所输入的的内容会以返回值得形式返回</p>
</li>
<li><p>注意：input()的返回值是一个字符串</p>
</li>
<li><p>input()函数中可以设置一个字符串作为参数，这个字符串将会作为提示文字显示</p>
</li>
<li><p>a = input(‘请输入任意内容：’)</p>
</li>
<li><p>print(‘用户输入的内容是:’,a)</p>
</li>
<li>input()也可以用于暂时阻止程序结束</li>
</ul>
<h2 id="3、if-else"><a href="#3、if-else" class="headerlink" title="3、if-else"></a>3、if-else</h2><ul>
<li><p>if-else：</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件表达式:</span><br><span class="line">    代码块</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    代码块</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="4、if-elif-else"><a href="#4、if-elif-else" class="headerlink" title="4、if-elif-else"></a>4、if-elif-else</h2><ul>
<li><p>if-elif-else</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#语法</span></span><br><span class="line"><span class="keyword">if</span> 条件表达式:</span><br><span class="line">    代码块</span><br><span class="line"><span class="keyword">elif</span> 条件表达式:</span><br><span class="line">    代码块</span><br><span class="line"><span class="keyword">elif</span> 条件表达式:</span><br><span class="line">    代码块</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    代码块</span><br></pre></td></tr></table></figure>
</li>
<li><p>执行流程if-elif-else语句在执行时，会自上向下依次对条件表达式进行求值判断，</p>
<ul>
<li>如果表达式的结果为True，则执行当前代码块，然后语句结束</li>
<li>如果表达式的结果为False，则继续向下判断，直到找到True为止</li>
<li>如果所有的表达式都是False，则执行else后的代码块</li>
</ul>
</li>
<li><p>if-elif-else中只会有一个代码块会执行</p>
</li>
<li><p>实现顺序由上至下，上面有代码满足条件的话，下面的代码将不会执行</p>
</li>
<li><p><strong>注意：写代码的时候不能写“ 死代码 ”：就是前面的条件包含下面的条件，此时下面的代码是永远不会执行的，这就称为“ 死代码，这是需要将代码修改”</strong></p>
</li>
</ul>
<h2 id="5、if-（if-else）-else"><a href="#5、if-（if-else）-else" class="headerlink" title="5、if-（if-else）-else"></a>5、if-（if-else）-else</h2><ul>
<li><p>格式</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件表达式:</span><br><span class="line">    代码块</span><br><span class="line">	<span class="keyword">if</span> 条件表达式:</span><br><span class="line">    	代码块</span><br><span class="line">    <span class="keyword">elif</span> 条件表达式:</span><br><span class="line">    	代码块</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        代码块</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    代码块</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="三、循环语句"><a href="#三、循环语句" class="headerlink" title="三、循环语句"></a>三、循环语句</h1><p>循环语句可以使指定的代码块重复指定的次数</p>
<p>循环语句分成两种，while循环 和 for循环</p>
<h2 id="1、while循环"><a href="#1、while循环" class="headerlink" title="1、while循环"></a>1、while循环</h2><ul>
<li><p>语法：</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> 条件表达式 :</span><br><span class="line">	代码块</span><br><span class="line"><span class="keyword">else</span> :</span><br><span class="line">	代码块</span><br><span class="line">    </span><br><span class="line"><span class="comment">#代码示例：</span></span><br><span class="line">a=<span class="number">1</span></span><br><span class="line"><span class="keyword">while</span>  a&lt;<span class="number">10</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;a =&#x27;</span>,a)</span><br><span class="line">    a+=<span class="number">1</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;else中的代码块&#x27;</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>执行流程：</p>
</li>
<li><p>while语句在执行时，会先对while后的条件表达式进行求值判断，</p>
<ul>
<li>如果判断结果为True，则执行循环体（代码块），</li>
<li>循环体执行完毕，继续对条件表达式进行求值判断，以此类推，</li>
<li>直到判断结果为False，则循环终止，如果循环有对应的else，则执行else后的代码块</li>
</ul>
</li>
<li><p>条件表达式恒为True的循环语句，称为死循环，它会一直运行，慎用！</p>
</li>
<li><p>循环的三个要件（表达式）</p>
<ul>
<li>初始化表达式，通过初始化表达式初始化一个变量</li>
<li>初始化表达式，通过初始化表达式初始化一个变量</li>
<li>更新表达式，修改初始化变量的值</li>
</ul>
</li>
</ul>
<h2 id="2、循环嵌套"><a href="#2、循环嵌套" class="headerlink" title="2、循环嵌套"></a>2、循环嵌套</h2><p><strong>外层循环执行一次，内层循环执行一遍</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">打印乘法口诀表</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">i = <span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> i &lt;= <span class="number">9</span>:</span><br><span class="line">    a = <span class="number">1</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 内层循环</span></span><br><span class="line">    <span class="keyword">while</span> a &lt;= i:</span><br><span class="line">        <span class="comment"># 判断是否需要换行输出：</span></span><br><span class="line">        <span class="keyword">if</span> a != i:</span><br><span class="line">            <span class="built_in">print</span>(a, <span class="string">&quot;*&quot;</span>, i, <span class="string">&quot;=&quot;</span>, <span class="built_in">int</span>(a * i), end=<span class="string">&quot;\t&quot;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(a, <span class="string">&quot;*&quot;</span>, i, <span class="string">&quot;=&quot;</span>, <span class="built_in">int</span>(a * i), end=<span class="string">&quot;\n&quot;</span>)</span><br><span class="line">        a += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">    i += <span class="number">1</span></span><br></pre></td></tr></table></figure>
<h2 id="3、break、continue和pass"><a href="#3、break、continue和pass" class="headerlink" title="3、break、continue和pass"></a>3、break、continue和pass</h2><ul>
<li><p>break可以用来立即退出循环语句（包括else），直接退出，不继续执行，直接终止该循环程序</p>
</li>
<li><p>continue可以用来跳过当次循环，结束本次循环，开始下次循环，该循环程序不结束</p>
</li>
<li><p>pass是用来在判断或循环语句中占位的</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#如使用了一个if语句：这个if语句里面不想执行其他语句，这时就可以使用pass来占位</span></span><br><span class="line"><span class="comment">#pass相当于占位的，程序不执行</span></span><br><span class="line"><span class="comment">#例如：</span></span><br><span class="line"><span class="keyword">if</span> 判断条件:</span><br><span class="line">    <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="4、模块"><a href="#4、模块" class="headerlink" title="4、模块"></a>4、模块</h2><ul>
<li><p>模块，通过模块可以对Python进行扩展</p>
</li>
<li><p>引入一个time模块，来统计程序执行的时间</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#先导入time 模块</span></span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> *</span><br><span class="line"><span class="comment">#使用方式</span></span><br><span class="line">begin = time()</span><br><span class="line">end = time()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;程序运行的时间为&quot;</span>,end-begin,<span class="string">&quot;秒&quot;</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>time()函数可以用来获取当前的时间，返回的单位是秒</p>
</li>
<li><p>获取程序开始的时间</p>
</li>
</ul>
<h1 id="四、序列"><a href="#四、序列" class="headerlink" title="四、序列"></a>四、序列</h1><h2 id="1、列表（list）的的简介"><a href="#1、列表（list）的的简介" class="headerlink" title="1、列表（list）的的简介"></a>1、列表（list）的的简介</h2><p>存取数据和Java中的lilst集合差不多</p>
<p><strong>列表是有序的可变的序列</strong></p>
<p>Java中的数组长度一旦定义了就不可变了，但是Python可以改变</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#Java创建数组，创建一个数组长度为10的object类型的数组</span></span><br><span class="line">Object[] obj = new Object[<span class="number">10</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">#Python创建数列</span></span><br><span class="line">my_list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">len</span>(my_list)) <span class="comment"># 3</span></span><br><span class="line"><span class="built_in">print</span>(my_list) <span class="comment"># [1, 2, 3]</span></span><br><span class="line"><span class="built_in">print</span>(my_list[<span class="number">1</span>]) <span class="comment"># 2</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(my_list)) <span class="comment"># &lt;class &#x27;list&#x27;&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>列表是Python中的一个对象</li>
<li>对象（object）就是内存中专门用来存储数据的一块区域</li>
<li>之前我们学习的对象，像数值，它只能保存一个单一的数据</li>
<li>列表中可以保存多个有序的数据</li>
<li>列表是用来存储对象的对象</li>
<li>列表的使用：<ul>
<li>列表的创建</li>
<li>操作列表中的数据</li>
</ul>
</li>
<li>练习：<ul>
<li>创建一个列表，在列表中保存你最好的5个朋友的名字</li>
<li>然后分别通过索引来获取每一个朋友的名字</li>
</ul>
</li>
</ul>
<h2 id="2、切片"><a href="#2、切片" class="headerlink" title="2、切片"></a>2、切片</h2><p>具体使用可以上网搜索：<strong>python的切片规则</strong></p>
<ul>
<li>从现有的集合中截取一部分数据，变为一个新的集合，可以设置截取的步长值。</li>
<li>切片指从现有列表中，获取一个子列表</li>
<li><p>创建一个列表，一般创建列表时，变量的名字会使用复数</p>
</li>
<li><p>列表的索引可以是负数（-1：表示倒数第一个数，-2：表示倒数第二个数）</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">my_list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="built_in">print</span>(my_list[-<span class="number">1</span>]) <span class="comment"># 3 </span></span><br><span class="line"><span class="built_in">print</span>(my_list[-<span class="number">2</span>]) <span class="comment"># 2</span></span><br><span class="line"><span class="built_in">print</span>(my_list[-<span class="number">3</span>]) <span class="comment"># 1</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>如果索引是负数，则从后向前获取元素，-1表示倒数第一个，-2表示倒数第二个 以此类推</p>
</li>
</ul>
<ul>
<li><p>通过切片来获取指定的元素</p>
</li>
<li><p>语法：列表[起始:结束] </p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">语法：列表[起始:结束] </span><br><span class="line">my_list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="built_in">print</span>(my_list[<span class="number">0</span>:<span class="number">2</span>]) <span class="comment">#输出：[1, 2]</span></span><br></pre></td></tr></table></figure>
<ul>
<li>通过切片获取元素时，会包括起始</li>
<li>位置的元素，不会包括结束位置的元素</li>
<li>做切片操作时，总会返回一个新的列表，不会影响原来的列表</li>
<li>起始和结束位置的索引都可以省略不写</li>
<li>如果省略结束位置，则会一直截取到最后</li>
<li>如果省略起始位置，则会从第一个元素开始截取</li>
<li>如果起始位置和结束位置全部省略，则相当于创建了一个列表的副本</li>
</ul>
</li>
<li><p>```<br>  语法：列表[起始:结束:步长] </p>
  <figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="bullet">    -</span> 步长：每隔多少个元素取一个元素</span><br><span class="line"><span class="bullet">    -</span> 步长表示，每次获取元素的间隔，默认值是1</span><br><span class="line"><span class="bullet">    -</span> 步长不能是0，但是可以是负数</span><br><span class="line"><span class="bullet">    -</span> 如果是负数，则会从列表的后部向前边取元素</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="section">## 3、列表的通用操作</span></span><br><span class="line"></span><br><span class="line">（ + ）和 （ * ）</span><br><span class="line"></span><br><span class="line"><span class="bullet">-</span> +可以将两个列表拼接为一个列表</span><br><span class="line"></span><br><span class="line"><span class="code">    ```python</span></span><br><span class="line"><span class="code">    my_list = [1, 2, 3] + [4, 5, 6]</span></span><br><span class="line"><span class="code">    print(my_list) # 输出条件：[1, 2, 3, 4, 5, 6]</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>( * )可以将列表重复指定的次数</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">my_list = [<span class="number">1</span>, <span class="number">2</span>]*<span class="number">3</span></span><br><span class="line"><span class="built_in">print</span>(my_list) <span class="comment">#输出：[1, 2, 1, 2, 1, 2]</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>in 和 not in</p>
<ul>
<li><p>in用来检查指定元素是否存在于列表中</p>
<ul>
<li>如果存在，返回True，否则返回False</li>
</ul>
</li>
<li><p>not in用来检查指定元素是否不在列表中</p>
<ul>
<li><p>如果不在，返回True，否则返回False</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">my_list = [<span class="number">1</span>, <span class="number">2</span>] * <span class="number">3</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">3</span> <span class="keyword">not</span> <span class="keyword">in</span> my_list)  <span class="comment"># 判断3不在my_list 集合中，输出：True</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">2</span> <span class="keyword">in</span> my_list)  <span class="comment"># 判断2在my_list集合中 ，输出：True</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>len()获取列表中的元素的个数</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">my_list = [<span class="number">1</span>, <span class="number">2</span>] * <span class="number">3</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">len</span>(my_list))  <span class="comment"># 判断集合中有几个元素 ，输出：6</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>min() 获取列表中的最小值</p>
</li>
<li><p>max() 获取列表中的最大值</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">my_list = [<span class="number">1</span>, <span class="number">2</span>] * <span class="number">3</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">min</span>(my_list)) <span class="comment"># 输出集合中的最小值：输出：1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">max</span>(my_list)) <span class="comment"># 输出集合中的最大值：输出：2</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>两个方法（method），方法和函数基本上是一样，只不过方法必须通过 对象.方法() 的形式调用</p>
<ul>
<li><p>list.index(x[, start[, end]])</p>
<ul>
<li>第一个参数：获取传入参数第一次出现的位置（索引）</li>
<li>第二个参数：从序列的第几个索引开始索引，到最后结束</li>
<li>第三个参数：需要前面两个参数都传入了值才能使用。表示，在哪个范围寻找传入参数的索引位置</li>
</ul>
</li>
<li><p>index()：获取指定元素在列表中第一次出现时的索引    </p>
<ul>
<li>例如：xxx.print（这是调用方法的形式）</li>
</ul>
</li>
</ul>
</li>
</ul>
<pre><code>    <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#使用 index() 方法</span></span><br></pre></td></tr></table></figure>

- count()：统计我们指定元素在列表中出现的次数
</code></pre><h2 id="4、序列"><a href="#4、序列" class="headerlink" title="4、序列"></a>4、序列</h2><ul>
<li>序列是Python中最基本的一种数据结构</li>
<li>数据结构指计算机中数据存储的方式</li>
<li>序列用于保存一组有序的数据，所有的数据在序列当中都有一个唯一的位置（索引）<ul>
<li>并且序列中的数据会按照添加的顺序来分配索引</li>
</ul>
</li>
<li>序列的分类：<ul>
<li>可变序列（序列中的元素可以改变）<ul>
<li>列表（list）</li>
</ul>
</li>
<li>不可变序列（序列中的元素不能改变）：<ul>
<li>字符串</li>
<li>元组</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="5、通过索引和切片修改元素"><a href="#5、通过索引和切片修改元素" class="headerlink" title="5、通过索引和切片修改元素"></a>5、通过索引和切片修改元素</h2><ul>
<li><p>通过索引和切片修改列表中的元素</p>
<ul>
<li>直接通过索引来修改元素</li>
<li>通过del来删除元素</li>
<li>通过切片来修改列表</li>
<li>通过切片来删除元素<ul>
<li>在给切片赋值时，只能使用序列</li>
<li>当设置了步长值时，序列中元素的个数必须和切片中元素个数一直</li>
</ul>
</li>
<li><p>以上操作，只适用于可变序列（字符串的底层就是序列，但字符串是不可变的）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">stu = [<span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;猪八戒&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>, <span class="string">&#x27;蜘蛛精&#x27;</span>, <span class="string">&#x27;白骨精&#x27;</span>]</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- 直接通过索引来修改元素</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">stu[<span class="number">2</span>] = <span class="string">&#x27;金角大王&#x27;</span></span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出：[&#x27;孙悟空&#x27;, &#x27;猪八戒&#x27;, &#x27;金角大王&#x27;, &#x27;唐僧&#x27;, &#x27;蜘蛛精&#x27;, &#x27;白骨精&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- 通过del来删除元素</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">del</span> stu[<span class="number">5</span>]</span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出：[&#x27;孙悟空&#x27;, &#x27;猪八戒&#x27;, &#x27;金角大王&#x27;, &#x27;唐僧&#x27;, &#x27;蜘蛛精&#x27;]</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- 通过切片来修改列表</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">stu[<span class="number">4</span>:<span class="number">5</span>] = [<span class="string">&quot;白龙马&quot;</span>, <span class="string">&quot;白骨精&quot;</span>]</span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出：[&#x27;孙悟空&#x27;, &#x27;猪八戒&#x27;, &#x27;金角大王&#x27;, &#x27;唐僧&#x27;, &#x27;白龙马&#x27;, &#x27;白骨精&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- 通过切片来删除元素</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">del</span> stu[<span class="number">4</span>: <span class="number">5</span>]</span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出：[&#x27;孙悟空&#x27;, &#x27;猪八戒&#x27;, &#x27;金角大王&#x27;, &#x27;唐僧&#x27;, &#x27;白骨精&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- 以上操作，只适用于可变序列</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">s = <span class="string">&quot;hello&quot;</span></span><br><span class="line">s[<span class="number">1</span>] = <span class="string">&quot;2&quot;</span>  <span class="comment"># 会报错：TypeError: &#x27;str&#x27; object does not support item assignment</span></span><br><span class="line"><span class="built_in">print</span>(s[<span class="number">1</span>])</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h2 id="6、通过方法修改元素"><a href="#6、通过方法修改元素" class="headerlink" title="6、通过方法修改元素"></a>6、通过方法修改元素</h2><ul>
<li><p>使用方法修改列表的元素</p>
<ul>
<li>append() ：向列表最后一行添加一个元素</li>
<li>insert()：向列表的指定位置插入一个元素<ul>
<li>参数：<ul>
<li>要插入的位置</li>
<li>要插入的元素</li>
</ul>
</li>
</ul>
</li>
<li>extend()：使用新的序列来扩展当前序列，在原有的基础上添加新的序列<ul>
<li>需要一个序列作为参数，它会将该序列中的元素添加到当前列表中</li>
</ul>
</li>
<li>clear()：清空序列</li>
<li>pop()：根据索引删除并返回被删除的元素<ul>
<li>传入一个int类型的参数表示<strong>删除指定索引位置</strong>的元素</li>
</ul>
</li>
<li>remove()：<strong>删除指定值</strong>的元素，如果相同值得元素有多个，只会删除第一个</li>
<li>reverse()：用来反转列表</li>
<li><p>sort()：用来对列表中的元素进行排序，<strong>默认是升序排列</strong>，如果需要降序<strong>排列</strong>，则需要<strong>传递一个reverse=True作为参数</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">stu = [<span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;猪八戒&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>]</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- append() ：向列表最后一行添加一个元素</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">stu.append(<span class="string">&quot;白龙马&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出为：[&#x27;孙悟空&#x27;, &#x27;猪八戒&#x27;, &#x27;沙和尚&#x27;, &#x27;唐僧&#x27;, &#x27;白龙马&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- insert()：向列表的指定位置插入一个元素</span></span><br><span class="line"><span class="string">    - 参数：</span></span><br><span class="line"><span class="string">        - 要插入的位置</span></span><br><span class="line"><span class="string">        - 要插入的元素</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">stu.insert(<span class="number">0</span>, <span class="string">&quot;牛魔王&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出为：[&#x27;牛魔王&#x27;, &#x27;孙悟空&#x27;, &#x27;猪八戒&#x27;, &#x27;沙和尚&#x27;, &#x27;唐僧&#x27;, &#x27;白龙马&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- extend()：使用新的序列来扩展当前序列</span></span><br><span class="line"><span class="string">    - 需要一个序列作为参数，它会将该序列中的元素添加到当前列表中 </span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">stu.extend([<span class="string">&quot;金角大王&quot;</span>, <span class="string">&quot;银角大王&quot;</span>])</span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出为：[&#x27;牛魔王&#x27;, &#x27;孙悟空&#x27;, &#x27;猪八戒&#x27;, &#x27;沙和尚&#x27;, &#x27;唐僧&#x27;, &#x27;白龙马&#x27;, &#x27;金角大王&#x27;, &#x27;银角大王&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- pop()：根据索引删除并返回被删除的元素</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(stu.pop(<span class="number">0</span>))  <span class="comment"># 输出为：牛魔王</span></span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出为：[&#x27;孙悟空&#x27;, &#x27;猪八戒&#x27;, &#x27;沙和尚&#x27;, &#x27;唐僧&#x27;, &#x27;白龙马&#x27;, &#x27;金角大王&#x27;, &#x27;银角大王&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- remove()：删除指定值得元素，如果相同值得元素有多个，只会删除第一个</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">stu.remove(<span class="string">&quot;白龙马&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出为：[&#x27;孙悟空&#x27;, &#x27;猪八戒&#x27;, &#x27;沙和尚&#x27;, &#x27;唐僧&#x27;, &#x27;金角大王&#x27;, &#x27;银角大王&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- reverse()：用来反转列表</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">stu.reverse()</span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出为：[&#x27;银角大王&#x27;, &#x27;金角大王&#x27;, &#x27;唐僧&#x27;, &#x27;沙和尚&#x27;, &#x27;猪八戒&#x27;, &#x27;孙悟空&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- sort()：用来对列表中的元素进行排序，默认是升序排列</span></span><br><span class="line"><span class="string">- 如果需要降序排列，则需要传递一个reverse=True作为参数</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">stu.sort(reverse=<span class="literal">True</span>)  <span class="comment"># 不传入reverse=True的话是默认升序排列，传入reverse=True是降序排列</span></span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出为：[&#x27;银角大王&#x27;, &#x27;金角大王&#x27;, &#x27;猪八戒&#x27;, &#x27;沙和尚&#x27;, &#x27;孙悟空&#x27;, &#x27;唐僧&#x27;]</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- clear()：清空序列</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">stu.clear()</span><br><span class="line"><span class="built_in">print</span>(stu)  <span class="comment"># 输出为：[]</span></span><br><span class="line"></span><br><span class="line">exit()</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h2 id="8、for循环"><a href="#8、for循环" class="headerlink" title="8、for循环"></a>8、for循环</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 语法</span></span><br><span class="line"><span class="keyword">for</span> 变量 <span class="keyword">in</span> 序列:</span><br><span class="line">    代码快</span><br></pre></td></tr></table></figure>
<ul>
<li>for循环的代码块会执行多次，序列中有几个元素就会执行几次<ul>
<li>每执行一次就会将序列中的一个元素赋值给变量</li>
<li>所以我们可以通过变量，来获取列表中的元素</li>
</ul>
</li>
<li>需要自己控制for循环的循环次数，可以使用 range（）函数来指定一个佛如循环的次数<ul>
<li>range() 函数可以指定一个序列</li>
</ul>
</li>
</ul>
<h2 id="7、遍历列表"><a href="#7、遍历列表" class="headerlink" title="7、遍历列表"></a>7、遍历列表</h2><ul>
<li><p>通过while循环来遍历列表（序列）</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">stu = [<span class="string">&#x27;孙悟空&#x27;</span>, <span class="string">&#x27;猪八戒&#x27;</span>, <span class="string">&#x27;沙和尚&#x27;</span>, <span class="string">&#x27;唐僧&#x27;</span>]</span><br><span class="line"></span><br><span class="line">i = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> i &lt; <span class="built_in">len</span>(stu):</span><br><span class="line">    <span class="built_in">print</span>(stu[i], end=<span class="string">&quot;\t&quot;</span>)</span><br><span class="line">    i += <span class="number">1</span></span><br><span class="line"><span class="comment"># 输出结果为：孙悟空	猪八戒	沙和尚	唐僧	</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>for循环来遍历列表（和Java中的foreach（增强for循环差不多使用方式））</p>
<ul>
<li>for循环的代码块会执行多次，序列中有几个元素就会执行几次</li>
<li>每执行一次就会将序列中的一个元素赋值给变量</li>
<li><p>所以我们可以通过变量，来获取列表中的元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> stu:</span><br><span class="line">    <span class="built_in">print</span>(i, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line"><span class="comment">#执行结果为：孙悟空 猪八戒 沙和尚 唐僧 白骨精 </span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h2 id="8、range（）函数"><a href="#8、range（）函数" class="headerlink" title="8、range（）函数"></a>8、range（）函数</h2><ul>
<li><p>range()是一个函数，可以用来<strong>生成一个自然数的序列</strong>，可以和for循环配合一起使用</p>
</li>
<li><p>该函数需要三个参数：取范围的时候（ begin &lt;= i &lt; end ） ，包括开始，不包括结束</p>
<ul>
<li>起始位置（可以省略，默认是0）</li>
<li>结束位置</li>
<li>步长（可以省略，默认是1）</li>
</ul>
</li>
<li><p>通过range()可以创建一个执行指定次数的for循环，for()循环除了创建方式以外，其余的都和while一样，</p>
<ul>
<li>包括else、包括break continue都可以在for循环中使用</li>
<li><p>并且for循环使用也更加简单</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 传入一个参数</span></span><br><span class="line">a = <span class="built_in">range</span>(<span class="number">3</span>)  <span class="comment"># 成成一个[0,1,2]序列</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line">    <span class="built_in">print</span>(i, end=<span class="string">&quot; &quot;</span>)  <span class="comment"># 0 1 2 </span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 传入两个参数</span></span><br><span class="line"><span class="built_in">range</span>(<span class="number">1</span>, <span class="number">3</span>)  <span class="comment"># 生成一个[1,2]序列</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 传入三个参数</span></span><br><span class="line"><span class="built_in">range</span>(<span class="number">1</span>, <span class="number">5</span>, <span class="number">2</span>)  <span class="comment"># 生成一个[1,3]序列</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h2 id="8、元组-不可变序列"><a href="#8、元组-不可变序列" class="headerlink" title="8、元组(不可变序列)"></a>8、元组(不可变序列)</h2><ul>
<li><p><strong>元组是有序的不可变的序列</strong></p>
</li>
<li><p><strong>tuple类型</strong></p>
</li>
<li><p>元组是一个不可变的序列，它的操作的方式基本上和列表是一致的</p>
<p>  所以你在操作元组时，就把元组当成是一个不可变的列表就ok了</p>
</li>
<li><p><strong>什么时候使用元组：</strong>一般当我们希望数据不改变时，就使用元组，其余情况都使用列表</p>
</li>
</ul>
<ul>
<li><p>创建元组</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建一个空元组</span></span><br><span class="line">my_tuple = ()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个有元素的元组</span></span><br><span class="line">my_tuple1 = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 当元组不是空元组时，括号可以省略</span></span><br><span class="line">my_tuple2 = <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果元组不是空元组，里面至少要有一个 ,</span></span><br><span class="line">my_tuple3 = <span class="number">20</span>,</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>元组的解包（解构）：解包指就是将元组当中每一个元素都赋值给一个变量</p>
<ul>
<li><p><strong>变量的个数需要和元组中元素的个数一样，如果不一样会报错（ValueError: too many values to unpack）</strong></p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">my_tuple2 = <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span></span><br><span class="line"><span class="comment">#此处变量的个数需要和元组中元素的个数一样</span></span><br><span class="line">a, b, c, d= my_tuple2</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, a)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, b)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;c =&quot;</span>, c)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;d =&quot;</span>, d)</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<pre><code>- **当想获取部分变量时候，也可以变量的数量和元组中元素的个数的数量不一样，**

    **也可以在变量前边添加一个*，这样变量将会获取元组中所有剩余的元素，（不能同时出现啷个 * 变量）**

    <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 当元组不是空元组时，括号可以省略</span></span><br><span class="line">my_tuple2 = <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#如下为在不同位置的变量上添加 * </span></span><br><span class="line"></span><br><span class="line">a, b, *c = my_tuple2</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, a)  <span class="comment"># 输出：a = 1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, b)  <span class="comment"># 输出：b = 2</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;c =&quot;</span>, c)  <span class="comment"># 输出：c = [3, 4]</span></span><br><span class="line"></span><br><span class="line">a, *b, c = my_tuple2</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, a)  <span class="comment"># 输出：a = 1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, b)  <span class="comment"># 输出：b = [2, 3]</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;c =&quot;</span>, c)  <span class="comment"># 输出：c = 4</span></span><br><span class="line"></span><br><span class="line">*a, b, c = my_tuple2</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, a)  <span class="comment"># 输出：a = [1, 2]</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, b)  <span class="comment"># 输出：b = 3</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;c =&quot;</span>, c)  <span class="comment"># 输出：c = 4</span></span><br></pre></td></tr></table></figure>



- 使用场景：

    - 场景一：交换两个变量的值的时候

    <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">100</span></span><br><span class="line">b = <span class="number">200</span></span><br><span class="line"><span class="built_in">print</span>(a, b)  <span class="comment"># 输出：100，200</span></span><br><span class="line">c = <span class="number">200</span></span><br><span class="line">d = <span class="number">100</span></span><br><span class="line">a, b = c, d</span><br><span class="line"><span class="built_in">print</span>(a, b)  <span class="comment"># 输出：200，100</span></span><br></pre></td></tr></table></figure>
</code></pre><h2 id="9、可变对象"><a href="#9、可变对象" class="headerlink" title="9、可变对象"></a>9、可变对象</h2><ul>
<li><p>该对象不影响id值，只修改了值。改变量是把id和value都修改了</p>
</li>
<li><p>每个对象中都保存了三个数据：</p>
<ul>
<li>id（标识）</li>
</ul>
</li>
<li><p>type（类型）</p>
<ul>
<li>value（值）    </li>
</ul>
</li>
</ul>
<ul>
<li><p>列表就是一个可变对象</p>
</li>
<li><p>a = [1,2,3]</p>
<p>​      </p>
</li>
<li><p>a[0] = 10 （改对象）</p>
<ul>
<li><p>这个操作是在通过变量去修改对象的值</p>
</li>
<li><p>这种操作不会改变变量所指向的对象    </p>
</li>
<li><p>当我们去修改对象时，如果有其他变量也指向了该对象，则修改也会在其他的变量中体现</p>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>a = [4,5,6] （改变量）</p>
<ul>
<li>这个操作是在给变量重新赋值</li>
<li>这种操作会改变变量所指向的对象</li>
<li>为一个变量重新赋值时，不会影响其他的变量</li>
</ul>
</li>
<li><p><strong>一般只有在为变量赋值时才是修改变量，其余的都是修改对象</strong></p>
</li>
</ul>
<ul>
<li><p>当修改列表的某一个值的时候是改对象，当给一个列表重新赋值的时候是改变量</p>
</li>
<li><p>图解</p>
<p>  <img src= "" data-lazy-src="https://www.naste.top:9000/webp/image-20210311150551926.webp" alt="image-20210311150551926"></p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a&quot;</span>, <span class="built_in">id</span>(a), a)  <span class="comment"># a 2118911262400 [1, 2, 3]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 改对象的值。id值没有改变。此时还是同一个变量</span></span><br><span class="line">a[<span class="number">0</span>] = <span class="number">4</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a&quot;</span>, <span class="built_in">id</span>(a), a)  <span class="comment"># a 2118911262400 [4, 2, 3]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 这里是改变量，给变量重新赋值，id值已经改变了，这两个变量不是一个对象了</span></span><br><span class="line">a = [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a&quot;</span>, <span class="built_in">id</span>(a), a)  <span class="comment"># a 2748994067584 [5, 6, 7]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将一个变量的值赋给另外一个变量，此时a和b是一个对象，</span></span><br><span class="line"><span class="comment"># 他们的id值都是相同的，此时改变一个变量中的值，另外一个变量的值也会修改</span></span><br><span class="line"></span><br><span class="line">b = a</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;b&quot;</span>, <span class="built_in">id</span>(b), b)  <span class="comment"># b 2748994067584 [5, 6, 7]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 因为此时变量a和变量b的id值都是相等的，此时修改了b变量中的值，a变量中的值也会修改</span></span><br><span class="line"><span class="comment"># 因为变量b的值是由变量a赋值的来的，此时修改的变量b中对象的值，而变量b的值没有修改</span></span><br><span class="line"><span class="comment"># 所以此时b的id值没有改变，所以变量a中的值也会变量b中对象的只修改而修改，</span></span><br><span class="line"><span class="comment"># 反之，变量b中对象的值也会随着变量a中对象的值的改变而改变</span></span><br><span class="line">b[<span class="number">1</span>] = <span class="number">0</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;b&quot;</span>, <span class="built_in">id</span>(b), b)  <span class="comment"># b 2748994067584 [5, 0, 7]</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a&quot;</span>, <span class="built_in">id</span>(a), a)  <span class="comment"># a 2748994067584 [5, 0, 7]</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="10、python中的-、-、is、is-not"><a href="#10、python中的-、-、is、is-not" class="headerlink" title="10、python中的==、!=、is、is not"></a>10、python中的==、!=、is、is not</h2><ul>
<li><p>== 和 != ：比较的是对象的值是否相等</p>
</li>
<li><p>is 和 is not 比较的是对象的id是否相等（比较两个对象是否是同一个对象）</p>
</li>
<li><p><strong>一般使用== 和 != 除非是比较严格的要求两个对象是否相等的时候才会使用 is 和 is not</strong></p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line">b = [<span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line"><span class="comment"># a和b的id分别为</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(a))  <span class="comment"># 2890673028800</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(b))  <span class="comment"># 2890970504256</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ” == 和 != “ 比较的是两个变量的值</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a和b是否相等：&quot;</span>, a == b)  <span class="comment"># True</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># ” is 和 is not “ 比较的是两个变量的id值，也就是内存地址</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;a和b是否相等：&quot;</span>, a <span class="keyword">is</span> b)  <span class="comment"># False</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="11、字典（dict）"><a href="#11、字典（dict）" class="headerlink" title="11、字典（dict）"></a>11、字典（dict）</h2><p><strong>和Java中的map集合差不多</strong></p>
<ul>
<li><p>字典属于一种新的数据结构，称为映射（mapping）</p>
</li>
<li><p>字典的作用和列表类似，都是用来存储对象的容器</p>
</li>
<li><p>列表存储数据的性能很好，但是查询数据的性能很差</p>
</li>
<li><p>在字典中每一个元素都有一个唯一的元素，通过这个唯一的名字可以快速的查找到指定的元素</p>
</li>
<li><p>查询元素时，字典的效率是非常快的</p>
</li>
<li><p>在字典中可以保存多个对象，每个对象都会有一个唯一的名字</p>
</li>
<li><p>在字典中可以保存多个对象，每个对象都会有一个唯一的名字</p>
<ul>
<li>这个唯一的名字，我们称其为键（key），通过key可以快速查询value</li>
<li>这个对象，我们称其为值（value）</li>
<li>所以字典，我们也称其为键值对（key-value）结构</li>
<li>每个字典中都可以有多个键值对，而每一个键值对我们称其为一项（item）</li>
</ul>
</li>
<li><p>使用方式：</p>
<ul>
<li><p>创建方式：</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 字典</span></span><br><span class="line"><span class="comment"># 使用&#123;&#125;来创建字典</span></span><br><span class="line">my_dictionary = &#123;&#125;  <span class="comment"># 创建了一个空字典</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个包含有数据的字典</span></span><br><span class="line">my_dictionary1 = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;韩信&quot;</span>, <span class="string">&quot;age&quot;</span>: <span class="string">&quot;18&quot;</span>, <span class="string">&quot;gender&quot;</span>: <span class="string">&quot;男&quot;</span>&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>语法：</p>
<ul>
<li>{key:value,key:value,key:value}</li>
<li><p>字典的值可以是任意对象</p>
</li>
<li><p>字典的键可以是任意的不可变对象（int、str、bool、tuple …），但是一般我们都会使用str</p>
</li>
<li><p>字典的键是不能重复的，如果出现重复的后边的会替换到前边的</p>
</li>
</ul>
</li>
<li><p>根据键来获取值 ：字典变量名字[key]</p>
<ul>
<li><p>如果使用了字段中不存在的key，会报错：<strong>KeyError</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建一个包含有数据的字典</span></span><br><span class="line">my_dictionary1 = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;韩信&quot;</span>, <span class="string">&quot;age&quot;</span>: <span class="string">&quot;18&quot;</span>, <span class="string">&quot;gender&quot;</span>: <span class="string">&quot;男&quot;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取数据的方式：字典变量名字[key]</span></span><br><span class="line"><span class="built_in">print</span>(my_dictionary1[<span class="string">&quot;name&quot;</span>])</span><br><span class="line"><span class="built_in">print</span>(my_dictionary1[<span class="string">&quot;age&quot;</span>])</span><br><span class="line"><span class="built_in">print</span>(my_dictionary1[<span class="string">&quot;gender&quot;</span>])</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="12、字典的使用"><a href="#12、字典的使用" class="headerlink" title="12、字典的使用"></a>12、字典的使用</h2><ol>
<li><p>创建字典</p>
<ul>
<li><p>直接创建字典</p>
</li>
<li><p>使用 dict () 函数来创建字典</p>
</li>
<li><p>也可以将一个包含有双值子序列的序列转换为字典，</p>
<ul>
<li>双值序列，序列中只有两个值：[(“name”, “韩信”), (“age”, “18”), (“gender”, “男”)]，<strong>每个序列有两个值</strong></li>
<li><p>子序列，如果序列中的元素也是序列，那么我们就称这个元素为子序列</p>
<ul>
<li>例如[(“name”, “韩信”), (“age”, “18”), (“gender”, “男”)]中的（”name”,”韩信”），就是一个子序列</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1、直接创建字典</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string"># 字典</span></span><br><span class="line"><span class="string"># 使用&#123;&#125;来创建字典</span></span><br><span class="line"><span class="string">my_dictionary = &#123;&#125;  # 创建了一个空字典</span></span><br><span class="line"><span class="string">#语法：&#123;key:value,key:value,key:value&#125;</span></span><br><span class="line"><span class="string">创建一个包含有数据的字典</span></span><br><span class="line"><span class="string">my_dictionary1 = &#123;&quot;name&quot;: &quot;韩信&quot;, &quot;age&quot;: &quot;18&quot;, &quot;gender&quot;: &quot;男&quot;&#125;</span></span><br><span class="line"><span class="string">print(my_dictionary1[&quot;name&quot;])</span></span><br><span class="line"><span class="string">print(my_dictionary1[&quot;age&quot;])</span></span><br><span class="line"><span class="string">print(my_dictionary1[&quot;gender&quot;])</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2、使用函数来创建字典</span></span><br><span class="line"><span class="comment"># 每一个参数都是一个键值对，参数名就是键，参数名就是值（这种方式创建的字典，key都是字符串）</span></span><br><span class="line">dictionary2 = <span class="built_in">dict</span>(name=<span class="string">&quot;韩信&quot;</span>, age=<span class="string">&quot;18&quot;</span>, gender=<span class="string">&quot;男&quot;</span>)</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;韩信&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="string">&#x27;18&#x27;</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125; &lt;<span class="keyword">class</span> <span class="string">&#x27;dict&#x27;</span>&gt;</span><br><span class="line"><span class="built_in">print</span>(dictionary, <span class="built_in">type</span>(dictionary)) </span><br><span class="line"></span><br><span class="line"><span class="comment"># 3、也可以将一个包含由双值子序列的序列转换为字典</span></span><br><span class="line"><span class="comment">#	双值子序列，序列中只有两个值，</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
</ol>
<ol>
<li><p>字典中的一些方法</p>
<ul>
<li><p>len()：获取字典中键值对的个数</p>
</li>
<li><p>in 检查字典中是否包含指定的键</p>
</li>
<li><p>not in 检查字典中是否不包含指定的键</p>
</li>
<li><p>字典名字[key]：获取字典中的值，根据键来获取值，通过[]来获取值时，如果键不存在，会抛出异常 KeyError</p>
</li>
<li><p>get(key[, default]) ：该方法用来根据键来获取字典中的值</p>
<ul>
<li>如果获取的键在字典中不存在，会返回None</li>
<li><p>也可以指定一个默认值，来作为第二个参数，这样获取不到值时将会返回默认值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">dictionary2 = <span class="built_in">dict</span>(name=<span class="string">&quot;韩信&quot;</span>, age=<span class="string">&quot;18&quot;</span>, gender=<span class="string">&quot;男&quot;</span>)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- len()：获取字典中键值对的个数</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">len</span>(dictionary2))  <span class="comment"># 3</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- in 检查字典中是否包含指定的键</span></span><br><span class="line"><span class="string">格式：a = &#x27;key&#x27; in 字典名字</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;name&quot;</span> <span class="keyword">in</span> dictionary2)  <span class="comment"># True</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- not in 检查字典中是否不包含指定的键</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;name&quot;</span> <span class="keyword">not</span> <span class="keyword">in</span> dictionary2)  <span class="comment"># False</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- 字典名字[key]：获取字典中的值，根据键来获取值，通过[]来获取值时，如果键不存在，会抛出异常 KeyError</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(dictionary2[<span class="string">&quot;name&quot;</span>])  <span class="comment"># 韩信</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- get(key[, default]) ：该方法用来根据键来获取字典中的值</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(dictionary2.get(<span class="string">&quot;gender&quot;</span>))  <span class="comment"># 男</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    - 如果获取的键在字典中不存在，会返回None</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(dictionary2.get(<span class="string">&quot;name_gender&quot;</span>))  <span class="comment"># None</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    - 也可以指定一个默认值，来作为第二个参数，这样获取不到值时将会返回默认值</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="comment"># 我是默认的值，key没有对应的value的时候，我会返回</span></span><br><span class="line"><span class="built_in">print</span>(dictionary2.get(<span class="string">&quot;name_gender&quot;</span>, <span class="string">&quot;我是默认的值，key没有对应的value的时候，我会返回&quot;</span>))</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
</ol>
<ol>
<li><p>修改字典</p>
<ul>
<li><p><strong>d[key] = value：根据key来修改对应的value</strong> </p>
<ul>
<li>如果key存在则覆盖，不存在则添加</li>
</ul>
</li>
<li><p><strong>setdefault(key[, default])：向字典中添加元素</strong></p>
<ul>
<li>可以用来向字典中添加key-value</li>
<li>如果key已经存在于字典中，则返回key的值，不会对字典做任何操作</li>
<li>如果key不存在，则向字典中添加这个key，并设置value</li>
</ul>
</li>
<li><p><strong>update([other])：添加一个字典，字典中有的元素会被更新，没有填追加到末尾</strong></p>
<ul>
<li>将其他的字典中的key-value添加到当前字典中</li>
<li>如果有重复的key，则后边的会替换到当前的</li>
</ul>
</li>
<li><p><strong>popitem()：随机删除一个元素</strong></p>
<ul>
<li>随机删除字典中的一个键值对，一般都会删除最后一个键值对</li>
<li>删除之后，它会将删除的key-value作为返回值返回</li>
<li>返回的是一个元组，元组中有两个元素，第一个元素是删除的key，第二个是删除的value</li>
<li>当使用popitem()删除一个空字典时，会抛出异常 KeyError: ‘popitem(): dictionary is empty’</li>
</ul>
</li>
<li><p><strong>pop(key[, default])：根据key删除元素</strong></p>
<ul>
<li>根据key删除字典中的key-value，会将被删除的value返回！，如果删除不存在的key，会抛出异常</li>
<li>如果指定了默认值，再删除不存在的key时，不会报错，而是直接返回默认值</li>
</ul>
</li>
<li><p><strong>clear()：</strong>用来清空字典</p>
</li>
<li><p><strong>copy()：</strong></p>
<ul>
<li>该方法用于对字典进行浅复制</li>
<li>复制以后的对象，和原对象是独立，修改一个不会影响另一个</li>
<li>注意，浅复制会简单复制对象内部的值，如果值也是一个可变对象，这个可变对象不会被复制</li>
<li><p>如果字典复制给另外一个字典的话是两个独立的对象，他们的id值是不一样的，但是字典里面还有字典的话，词典里面的字典的id值是一样的此时，修改一个字典里面的字典的值，复制的字典里面字典的值也会被修改，因为两者id是一样的</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">dictionary = &#123;<span class="string">&quot;name&quot;</span>: &#123;<span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>, <span class="string">&quot;class&quot;</span>: <span class="string">&quot;计软19-1班&quot;</span>&#125;, <span class="string">&quot;age&quot;</span>: <span class="string">&quot;18&quot;</span>&#125;</span><br><span class="line">dictionary2 = dictionary.copy()</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;修改前dictionary的值：&quot;</span>, dictionary, <span class="string">&quot;\n整个字典的id=&quot;</span>, <span class="built_in">id</span>(dictionary))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;===================================================================&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;修改前dictionary2的值：&quot;</span>, dictionary2, <span class="string">&quot;\n整个字典的id=&quot;</span>, <span class="built_in">id</span>(dictionary2))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;===================================================================&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 此时修改dictionary中 key:name对应的字典中的id的值</span></span><br><span class="line">dictionary[<span class="string">&quot;name&quot;</span>][<span class="string">&quot;id&quot;</span>] = <span class="string">&quot;2&quot;</span> <span class="comment"># 修改字典中字典name中的id的值</span></span><br><span class="line">dictionary[<span class="string">&quot;age&quot;</span>] = <span class="string">&quot;20&quot;</span> <span class="comment"># 修改字典中的age</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;修改后dictionary的值：&quot;</span>, dictionary, <span class="string">&quot;\n整个字典的id=&quot;</span>, <span class="built_in">id</span>(dictionary))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;===================================================================&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;修改后dictionary2的值：&quot;</span>, dictionary2, <span class="string">&quot;\n整个字典的id=&quot;</span>, <span class="built_in">id</span>(dictionary2))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;===================================================================&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;dictionary 字典中的字典的id的值为：&quot;</span>, <span class="built_in">id</span>(dictionary[<span class="string">&quot;name&quot;</span>]))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;dictionary2字典中的字典的id的值为：&quot;</span>, <span class="built_in">id</span>(dictionary2[<span class="string">&quot;name&quot;</span>]))</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">修改前dictionary的值： &#123;&#x27;name&#x27;: &#123;&#x27;id&#x27;: &#x27;1&#x27;, &#x27;class&#x27;: &#x27;计软19-1班&#x27;&#125;, &#x27;age&#x27;: &#x27;18&#x27;&#125; </span></span><br><span class="line"><span class="string">整个字典的id= 1480807631360</span></span><br><span class="line"><span class="string">===================================================================</span></span><br><span class="line"><span class="string">修改前dictionary2的值： &#123;&#x27;name&#x27;: &#123;&#x27;id&#x27;: &#x27;1&#x27;, &#x27;class&#x27;: &#x27;计软19-1班&#x27;&#125;, &#x27;age&#x27;: &#x27;18&#x27;&#125; </span></span><br><span class="line"><span class="string">整个字典的id= 1480807630976</span></span><br><span class="line"><span class="string">===================================================================</span></span><br><span class="line"><span class="string">修改后dictionary的值： &#123;&#x27;name&#x27;: &#123;&#x27;id&#x27;: &#x27;2&#x27;, &#x27;class&#x27;: &#x27;计软19-1班&#x27;&#125;, &#x27;age&#x27;: &#x27;20&#x27;&#125; </span></span><br><span class="line"><span class="string">整个字典的id= 1480807631360</span></span><br><span class="line"><span class="string">===================================================================</span></span><br><span class="line"><span class="string">修改后dictionary2的值： &#123;&#x27;name&#x27;: &#123;&#x27;id&#x27;: &#x27;2&#x27;, &#x27;class&#x27;: &#x27;计软19-1班&#x27;&#125;, &#x27;age&#x27;: &#x27;18&#x27;&#125; </span></span><br><span class="line"><span class="string">整个字典的id= 1480807630976</span></span><br><span class="line"><span class="string">===================================================================</span></span><br><span class="line"><span class="string">dictionary 字典中的字典的id的值为： 1480807552512</span></span><br><span class="line"><span class="string">dictionary2字典中的字典的id的值为： 1480807552512</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
</li>
<li><p>字典方法的使用</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">dictionary2 = <span class="built_in">dict</span>(name=<span class="string">&quot;韩信&quot;</span>, age=<span class="string">&quot;18&quot;</span>, gender=<span class="string">&quot;男&quot;</span>)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- **d[key] = value** 如果key存在则覆盖，不存在则添加</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">dictionary2[<span class="string">&#x27;name&#x27;</span>] = <span class="string">&quot;李白&quot;</span></span><br><span class="line"><span class="built_in">print</span>(dictionary2)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- **setdefault(key[, default])：** 可以用来向字典中添加key-value</span></span><br><span class="line"><span class="string">    - 如果key已经存在于字典中，则返回key的值，不会对字典做任何操作</span></span><br><span class="line"><span class="string">    - 如果key不存在，则向字典中添加这个key，并设置value，不设置默认值的话，自动会被设置为None</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">dictionary2.setdefault(<span class="string">&quot;id&quot;</span>, <span class="string">&quot;1&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(dictionary2)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">-   update([other])：</span></span><br><span class="line"><span class="string">    - 没有对应的key就会添加到最后面，有key的话会更新当前key对应的value</span></span><br><span class="line"><span class="string">    - 将其他的字典中的key-value添加到当前字典中</span></span><br><span class="line"><span class="string">    - 如果有重复的key，则后边的会替换到当前的，当前的value将会被更新</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">dictionary = &#123;<span class="string">&quot;id&quot;</span>: <span class="string">&quot;2&quot;</span>, <span class="string">&quot;class&quot;</span>: <span class="string">&quot;计软19-1班&quot;</span>&#125;</span><br><span class="line">dictionary2.update(dictionary)</span><br><span class="line"><span class="built_in">print</span>(dictionary2)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- **popitem()：**</span></span><br><span class="line"><span class="string">    - 随机删除字典中的一个键值对，一般都会删除最后一个键值对</span></span><br><span class="line"><span class="string">    - 删除之后，它会将删除的key-value作为返回值返回</span></span><br><span class="line"><span class="string">    - 返回的是一个元组，元组中有两个元素，第一个元素是删除的key，第二个是删除的value</span></span><br><span class="line"><span class="string">    - 当使用popitem()删除一个空字典时，会抛出异常 KeyError: &#x27;popitem(): dictionary is empty&#x27;</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(dictionary2.popitem())  <span class="comment"># 被删除后返回的结果为：(&#x27;class&#x27;, &#x27;计软19-1班&#x27;)</span></span><br><span class="line"><span class="built_in">print</span>(dictionary2)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- **pop(key[, default])：**</span></span><br><span class="line"><span class="string">    - 根据key删除字典中的key-value，会将被删除的value返回！，如果删除不存在的key，会抛出异常</span></span><br><span class="line"><span class="string">    - 如果指定了默认值，再删除不存在的key时，不会报错，而是直接返回默认值</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(dictionary2.pop(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;这是默认值&quot;</span>))</span><br><span class="line"><span class="built_in">print</span>(dictionary2)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- **clear()：**用来清空字典</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">dictionary2.clear()</span><br><span class="line"><span class="built_in">print</span>(dictionary2)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">- **copy()：**</span></span><br><span class="line"><span class="string">    - 该方法用于对字典进行浅复制</span></span><br><span class="line"><span class="string">    - 复制以后的对象，和原对象是独立，修改一个不会影响另一个</span></span><br><span class="line"><span class="string">    -  注意，浅复制会简单复制对象内部的值，如果值也是一个可变对象，这个可变对象不会被复制</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line">dictionary = &#123;<span class="string">&quot;name&quot;</span>: &#123;<span class="string">&quot;id&quot;</span>: <span class="string">&quot;1&quot;</span>, <span class="string">&quot;class&quot;</span>: <span class="string">&quot;计软19-1班&quot;</span>&#125;, <span class="string">&quot;age&quot;</span>: <span class="string">&quot;18&quot;</span>&#125;</span><br><span class="line">dictionary2 = dictionary.copy()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;修改前dictionary的值：&quot;</span>, dictionary, <span class="string">&quot;\n整个字典的id=&quot;</span>, <span class="built_in">id</span>(dictionary))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;===================================================================&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;修改前dictionary2的值：&quot;</span>, dictionary2, <span class="string">&quot;\n整个字典的id=&quot;</span>, <span class="built_in">id</span>(dictionary2))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;===================================================================&quot;</span>)</span><br><span class="line"><span class="comment"># 此时修改dictionary中 key:name对应的字典中的id的值</span></span><br><span class="line">dictionary[<span class="string">&quot;name&quot;</span>][<span class="string">&quot;id&quot;</span>] = <span class="string">&quot;2&quot;</span></span><br><span class="line">dictionary[<span class="string">&quot;age&quot;</span>] = <span class="string">&quot;20&quot;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;修改后dictionary的值：&quot;</span>, dictionary, <span class="string">&quot;\n整个字典的id=&quot;</span>, <span class="built_in">id</span>(dictionary))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;===================================================================&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;修改后dictionary2的值：&quot;</span>, dictionary2, <span class="string">&quot;\n整个字典的id=&quot;</span>, <span class="built_in">id</span>(dictionary2))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;===================================================================&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;dictionary 字典中的字典的id的值为：&quot;</span>, <span class="built_in">id</span>(dictionary[<span class="string">&quot;name&quot;</span>]))</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;dictionary2字典中的字典的id的值为：&quot;</span>, <span class="built_in">id</span>(dictionary2[<span class="string">&quot;name&quot;</span>]))</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="13、字典的遍历"><a href="#13、字典的遍历" class="headerlink" title="13、字典的遍历"></a>13、字典的遍历</h2><ol>
<li>keys()<ul>
<li>该方法会返回一个序列，序列中保存有字典的所有的键</li>
<li>通过遍历keys()来获取所有的键</li>
</ul>
</li>
<li>values()<ul>
<li>该方法会返回一个序列，序列中保存有字典的左右的值</li>
</ul>
</li>
<li>items()<ul>
<li>该方法会返回字典中所有的项</li>
<li>它会返回一个序列，序列中包含有双值子序列</li>
<li>双值分别是，字典中的key和value</li>
</ul>
</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">dictionary = <span class="built_in">dict</span>(name=<span class="string">&quot;韩信&quot;</span>, age=<span class="string">&quot;18&quot;</span>, gender=<span class="string">&quot;男&quot;</span>)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">1. keys()</span></span><br><span class="line"><span class="string">    - 该方法会返回一个序列，序列中保存有字典的所有的键</span></span><br><span class="line"><span class="string">    - 通过遍历keys()来获取所有的键</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> dictionary.keys():</span><br><span class="line">    <span class="built_in">print</span>(i, end=<span class="string">&quot; &quot;</span>)  <span class="comment"># name age gender</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">2. values()</span></span><br><span class="line"><span class="string">    - 该方法会返回一个序列，序列中保存有字典的左右的值</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> dictionary.values():</span><br><span class="line">    <span class="built_in">print</span>(i, end=<span class="string">&quot; &quot;</span>)  <span class="comment"># 韩信 18 男</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">3. items()</span></span><br><span class="line"><span class="string">    - 该方法会返回字典中所有的项</span></span><br><span class="line"><span class="string">    - 它会返回一个序列，序列中包含有双值子序列</span></span><br><span class="line"><span class="string">    - 双值分别是，字典中的key和value</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将返回的双值子序列遍历</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> dictionary.items():</span><br><span class="line">    <span class="built_in">print</span>(i, end=<span class="string">&quot; &quot;</span>)  <span class="comment"># (&#x27;name&#x27;, &#x27;韩信&#x27;) (&#x27;age&#x27;, &#x27;18&#x27;) (&#x27;gender&#x27;, &#x27;男&#x27;) </span></span><br><span class="line">    </span><br><span class="line"><span class="comment"># 通过双层for循环将返回的双值子序列循环遍历出来，包括key和value</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n&quot;</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> dictionary.items():</span><br><span class="line">    <span class="keyword">for</span> a <span class="keyword">in</span> i:</span><br><span class="line">        <span class="built_in">print</span>(a, end=<span class="string">&quot; &quot;</span>)  <span class="comment"># name 韩信 age 18 gender 男</span></span><br><span class="line">        </span><br><span class="line"><span class="comment"># 通过一层循环将返回的双值子序列循环遍历出来，包括key和value</span></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> dictionary.items():</span><br><span class="line">    <span class="built_in">print</span>(k, <span class="string">&quot;=&quot;</span>, v) <span class="comment"># name = 韩信 age = 18 gender = 男 </span></span><br></pre></td></tr></table></figure>
<h2 id="14、集合（set）"><a href="#14、集合（set）" class="headerlink" title="14、集合（set）"></a>14、集合（set）</h2><ul>
<li><p>集合和列表非常相似</p>
<ul>
<li>不同点：<ul>
<li>集合中只能存储不可变对象（不可变对象：int，string，float，number，tuple（元组））</li>
<li>集合中存储的对象是无序（不是按照元素的插入顺序保存）</li>
<li>集合中不能出现重复的元素，出现重复的元素只会保存第一次出现的元素，之后出现的会被删除</li>
</ul>
</li>
</ul>
</li>
<li><p>集合创建</p>
<ul>
<li><p>使用 {} 来创建集合</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用 &#123;&#125; 来创建空集合类型为dict，为字典类型</span></span><br><span class="line">s = &#123;&#125;</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(s))  <span class="comment"># &lt;class &#x27;dict&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用 &#123;&#125; 来创建有数据的集合，此时的集合的类型为set，为集合类型</span></span><br><span class="line">s = &#123;<span class="number">10</span>, <span class="number">1</span>, <span class="number">23</span>, <span class="number">1</span>&#125;</span><br><span class="line"><span class="built_in">print</span>(s, <span class="string">&quot;type=&quot;</span>, <span class="built_in">type</span>(s))  <span class="comment"># &#123;1, 10, 23&#125; type= &lt;class &#x27;set&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line">s = &#123;<span class="number">10</span>, <span class="number">1</span>, <span class="number">23</span>, <span class="number">1</span>, &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;韩信&quot;</span>&#125;&#125;</span><br><span class="line"><span class="built_in">print</span>(s)  <span class="comment"># TypeError: unhashable type: &#x27;dict&#x27;  #只能存储不可变对象字典为可变对象</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<pre><code>- 通过set()来将序列和字典转换为集合

    - 使用set()将字典转换为集合时，只会包含字典中的键

    <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">s = <span class="built_in">set</span>()  <span class="comment"># 空集合</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;type =&quot;</span>, <span class="built_in">type</span>(s))  <span class="comment"># type = &lt;class &#x27;set&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过set(将序列和字典转换为集合)</span></span><br><span class="line">s = <span class="built_in">set</span>(&#123;<span class="string">&quot;name&quot;</span>, <span class="number">1</span>, <span class="string">&quot;韩信&quot;</span>&#125;)  <span class="comment"># 通过set将列表转化为集合</span></span><br><span class="line"><span class="built_in">print</span>(s, <span class="string">&quot; type =&quot;</span>, <span class="built_in">type</span>(s))  <span class="comment"># &#123;&#x27;韩信&#x27;, 1, &#x27;name&#x27;&#125;  type = &lt;class &#x27;set&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line">s = <span class="built_in">set</span>(&#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;李白&quot;</span>, <span class="string">&quot;gender&quot;</span>: <span class="string">&quot;男&quot;</span>&#125;)  <span class="comment"># 通过set将字典转化为集合</span></span><br><span class="line"><span class="built_in">print</span>(s, <span class="string">&quot;type =&quot;</span>, <span class="built_in">type</span>(s))  <span class="comment"># &#123;&#x27;name&#x27;, &#x27;gender&#x27;&#125; type = &lt;class &#x27;set&#x27;&gt;</span></span><br></pre></td></tr></table></figure>
</code></pre><ul>
<li><p>集合中的方法</p>
<ul>
<li>使用in和not in来检查集合中的元素</li>
<li>使用len()来获取集合中元素的数量</li>
<li>add() 向集合中添加元素</li>
<li>update() 将一个集合中的元素添加到当前集合中</li>
<li>update()可以传递序列或字典作为参数，字典只会使用键</li>
<li>pop()随机删除并返回一个集合中的元素</li>
<li>remove()删除集合中的指定元素</li>
<li>clear()清空集合</li>
<li><p>copy()对集合进行浅复制</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">my_list = &#123;<span class="number">1</span>, <span class="number">23</span>, <span class="number">123</span>, <span class="number">890</span>, <span class="number">1230</span>, <span class="number">100</span>, <span class="number">23</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># - 使用in和not in来检查集合中的元素</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">123</span> <span class="keyword">in</span> my_list)  <span class="comment"># True</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - 使用len()来获取集合中元素的数量</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">len</span>(my_list))  <span class="comment"># 6</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - add() 向集合中添加元素</span></span><br><span class="line">my_list.add(<span class="number">190</span>)</span><br><span class="line"><span class="built_in">print</span>(my_list, <span class="string">&quot;len =&quot;</span>, <span class="built_in">len</span>(my_list))  <span class="comment"># &#123;1, 100, 1230, 23, 890, 123, 190&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - update() 将一个集合中的元素添加到当前集合中</span></span><br><span class="line">my_list.update(&#123;<span class="number">20</span>, <span class="number">300</span>&#125;)</span><br><span class="line"><span class="built_in">print</span>(my_list, <span class="string">&quot;len =&quot;</span>, <span class="built_in">len</span>(my_list))  <span class="comment"># &#123;1, 100, 300, 1230, 20, 23, 890, 123, 190&#125; len = 9</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - update()可以传递序列或字典作为参数，字典只会使用键</span></span><br><span class="line">my_list.update(&#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;李白&quot;</span>, <span class="string">&quot;gender&quot;</span>: <span class="string">&quot;男&quot;</span>, <span class="string">&quot;age&quot;</span>: <span class="number">18</span>&#125;)</span><br><span class="line"><span class="built_in">print</span>(my_list, <span class="string">&quot;len =&quot;</span>, <span class="built_in">len</span>(my_list))</span><br><span class="line"><span class="comment"># &#123;1, 100, 300, 1230, &#x27;gender&#x27;, &#x27;name&#x27;, &#x27;age&#x27;, 20, 23, 890, 123, 190&#125; len = 12</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - pop()随机删除并返回一个集合中的元素</span></span><br><span class="line"><span class="built_in">print</span>(my_list.pop())  <span class="comment"># 删除一个随机元素</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - remove()删除集合中的指定元素</span></span><br><span class="line">my_list.remove(<span class="string">&quot;age&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(my_list, <span class="string">&quot;len =&quot;</span>, <span class="built_in">len</span>(my_list))</span><br><span class="line"><span class="comment"># &#123;100, 300, 1230, &#x27;name&#x27;, &#x27;gender&#x27;, 20, 23, 890, 123, 190&#125; len = 10</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - copy()对集合进行浅复制</span></span><br><span class="line">b = my_list.copy()</span><br><span class="line"><span class="built_in">print</span>(b, <span class="string">&quot;len =&quot;</span>, <span class="built_in">len</span>(b))</span><br><span class="line"><span class="comment"># &#123;1, 100, &#x27;name&#x27;, 300, 1230, 20, 23, 890, 123, 190&#125; len = 10</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># - clear()清空集合</span></span><br><span class="line">my_list.clear()</span><br><span class="line"><span class="built_in">print</span>(my_list, <span class="string">&quot;len =&quot;</span>, <span class="built_in">len</span>(my_list))  <span class="comment"># set() len = 0</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<ul>
<li>集合的运算，在对集合做运算时，不会影响原来的集合，而是返回一个运算结果<ul>
<li>&amp; 交集运算：两个集合中共同的部分</li>
<li>| 并集运算</li>
<li>（-） 差集</li>
<li>^ 异或集 获取只在一个集合中出现的元素</li>
<li>&lt;= 检查一个集合是否是另一个集合的子集<ul>
<li>如果a集合中的元素全部都在b集合中出现，那么a集合就是b集合的子集，b集合是a集合超集</li>
</ul>
</li>
<li>&lt; 检查一个集合是否是另一个集合的真子集<ul>
<li>如果超集b中含有子集a中所有元素，并且b中还有a中没有的元素，则b就是a的真超集，a是b的真子集</li>
</ul>
</li>
<li>（&gt;=） 检查一个集合是否是另一个的超集</li>
<li>（&gt;） 检查一个集合是否是另一个的真超集</li>
</ul>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在对集合做运算时，不会影响原来的集合，而是返回一个运算结果</span></span><br><span class="line"><span class="comment"># 创建两个集合</span></span><br><span class="line">s = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">s2 = &#123;<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#  &amp; 交集运算：两个集合中共同的部分</span></span><br><span class="line">result = s &amp; s2</span><br><span class="line"><span class="built_in">print</span>(result)  <span class="comment"># &#123;3, 4, 5&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  | 并集运算</span></span><br><span class="line">result = s | s2</span><br><span class="line"><span class="built_in">print</span>(result)  <span class="comment"># &#123;1, 2, 3, 4, 5, 6, 7&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># （-） 差集</span></span><br><span class="line">result = s - s2</span><br><span class="line"><span class="built_in">print</span>(result)  <span class="comment"># &#123;1, 2&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  ^ 异或集 获取只在一个集合中出现的元素</span></span><br><span class="line">result = s ^ s2</span><br><span class="line"><span class="built_in">print</span>(result)  <span class="comment"># &#123;1, 2, 6, 7&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  &lt;= 检查一个集合是否是另一个集合的子集</span></span><br><span class="line"><span class="comment">#  如果a集合中的元素全部都在b集合中出现，那么a集合就是b集合的子集，b集合是a集合超集</span></span><br><span class="line">result = s &lt;= s2</span><br><span class="line"><span class="built_in">print</span>(result)  <span class="comment"># False</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  &lt; 检查一个集合是否是另一个集合的真子集</span></span><br><span class="line">result = s &lt; s2</span><br><span class="line"><span class="built_in">print</span>(result)  <span class="comment"># False</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># （&gt;=） 检查一个集合是否是另一个的超集</span></span><br><span class="line">result = s &gt;= s2</span><br><span class="line"><span class="built_in">print</span>(result)  <span class="comment"># False</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># （&gt;） 检查一个集合是否是另一个的真超集</span></span><br><span class="line">result = s &gt; s2</span><br><span class="line"><span class="built_in">print</span>(result)  <span class="comment"># False</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="15、可变对象和不可变对象分别有哪些"><a href="#15、可变对象和不可变对象分别有哪些" class="headerlink" title="15、可变对象和不可变对象分别有哪些"></a>15、可变对象和不可变对象分别有哪些</h2><ul>
<li><strong>不可变（immutable）：int、字符串(string)、float、（数值型number）、元组（tuple)</strong></li>
<li><strong>可变（mutable）：字典型(dictionary)、列表型(list)</strong></li>
</ul>
<h1 id="五、函数"><a href="#五、函数" class="headerlink" title="五、函数"></a>五、函数</h1><h2 id="1、函数简介"><a href="#1、函数简介" class="headerlink" title="1、函数简介"></a>1、函数简介</h2><ul>
<li><p>函数也是一个对象</p>
</li>
<li><p>对象是内存中专门用来存储数据的一块区域。</p>
</li>
<li><p>函数可以用来保存一些可执行的代码，并且可以在需要时，对这些语句进行多次的调用</p>
</li>
</ul>
<h2 id="2、创建函数"><a href="#2、创建函数" class="headerlink" title="2、创建函数"></a>2、创建函数</h2><ul>
<li><p>创建函数：</p>
<ul>
<li><p>def 函数名([形参1,形参2,…形参n]) :</p>
<p>  ​                    代码块</p>
</li>
<li><p>函数名必须要符号标识符的规范：（可以包含字母、数字、下划线、但是不能以数字开头）   </p>
</li>
<li><p>函数中保存的代码不会立即执行，需要调用函数代码才会执行</p>
</li>
<li><p>调用函数：函数对象()</p>
</li>
<li><p>定义函数一般都是要实现某种功能的    </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#    - 练习1:定义一个函数，可以用来求任意三个数的乘积</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">product</span>(<span class="params">a, b, c</span>):</span><br><span class="line">    <span class="built_in">print</span>(a * b * c)</span><br><span class="line"></span><br><span class="line">product(<span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>)  <span class="comment"># 12</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#	练习2：定义一个函数，可以根据不同的用户名显示不同的欢迎信息    </span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">display</span>(<span class="params">username</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&#x27;欢迎<span class="subst">&#123;username&#125;</span>登录&#x27;</span>)</span><br><span class="line"></span><br><span class="line">display(<span class="string">&quot;张三&quot;</span>)  <span class="comment"># 欢迎张三登录</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h2 id="3、函数的参数"><a href="#3、函数的参数" class="headerlink" title="3、函数的参数"></a>3、函数的参数</h2><ul>
<li><p>定义形参时，可以为形参指定默认值，指定了默认值以后，如果用户传递了参数则默认值没有任何作用，反之则默认值就会生效</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">my_sum</span>(<span class="params">a, b=<span class="number">10</span></span>):  <span class="comment"># 此时10为b的默认值</span></span><br><span class="line">    <span class="built_in">print</span>(a + b)</span><br><span class="line"></span><br><span class="line">my_sum(<span class="number">10</span>)  <span class="comment"># ，没有输入参数足够的此时使用函数的默认值，输出为 20</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>实参的传递方式</p>
<ul>
<li>位置参数<ul>
<li>位置参数就是将对应位置的实参复制给对应位置的形参</li>
<li>第一个实参赋值给第一个形参，第二个实参赋值给第二个形参 。。。</li>
</ul>
</li>
<li><p>关键字参数</p>
<ul>
<li>关键字参数，可以不按照形参定义的顺序去传递，而直接根据参数名去传递参数</li>
<li>位置参数和关键字参数可以混合使用</li>
<li>合使用关键字和位置参数时，必须将位置参数写到前面</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">my_sum</span>(<span class="params">a, b</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a + b =&quot;</span>, a + b)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 位置参数，根据传入参数的位置去赋值给函数赋值</span></span><br><span class="line">my_sum(<span class="number">1</span>, <span class="number">2</span>)  <span class="comment"># a + b = 3</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 关键字参数：根据函数的参数的名字来给参数赋值，位置参数和关键字参数可以混合使用</span></span><br><span class="line">my_sum(<span class="number">2</span>, b=<span class="number">10</span>)	<span class="comment"># a + b = 12</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 位置参数找到对应的位置赋值之后，关键字参数不能再给位置参数赋值赋值的参数继续赋值</span></span><br><span class="line">my_sum(<span class="number">2</span>, a=<span class="number">10</span>)  <span class="comment"># TypeError: my_sum() got multiple values for argument &#x27;a&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<ul>
<li>函数在调用时，解析器不会检查实参的类型，实参可以传递任意类型的对象，<strong>由于字符串可以传入任意类型，所以方法中可能会因为传入的参数报异常</strong></li>
</ul>
<ul>
<li><p>在函数中对形参进行重新赋值，不会影响其他的变量，<strong>如果形参执行的是一个对象，当我们通过形参去修改对象时，会影响到所有指向该对象的变量</strong></p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建一个函数，用于修改列表的值</span></span><br><span class="line"><span class="comment">#此时由于传入的是一个	，当在函数中修改的时候，外面的传入的实参的值也会被修改</span></span><br><span class="line"><span class="comment">#想要值函数外面传入的值不被修改，可以使用浅复制，将列表浅复制的值传入函数中</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">update</span>(<span class="params">a</span>):</span><br><span class="line">    a[<span class="number">0</span>] = <span class="number">200</span></span><br><span class="line"></span><br><span class="line">a = [<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>]</span><br><span class="line">update(a)</span><br><span class="line"><span class="built_in">print</span>(a)  <span class="comment"># [200, 20, 30]</span></span><br><span class="line"></span><br><span class="line">b = [<span class="number">100</span>, <span class="number">300</span>, <span class="number">500</span>]</span><br><span class="line">update(b.copy())</span><br><span class="line"><span class="built_in">print</span>(b)  <span class="comment"># [100, 300, 500]</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="4、不定长参数（和Java中的可变参数差不多）"><a href="#4、不定长参数（和Java中的可变参数差不多）" class="headerlink" title="4、不定长参数（和Java中的可变参数差不多）"></a>4、不定长参数（和Java中的可变参数差不多）</h2><ul>
<li><p>在定义函数时，可以在形参前边加上一个*，这样这个形参将会获取到所有的实参，它将会将所有的实参保存到一个元组中</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 和元组的解包差不多</span></span><br><span class="line">a, b, *c = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line"><span class="built_in">print</span>(a, b, c)  <span class="comment"># 1 2 [3, 4]</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>*a会接受所有的位置实参，并且会将这些实参统一保存到一个元组中（装包）</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># *a会接受所有的位置实参，并且会将这些实参统一保存到一个元组中（装包）</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">my_fn</span>(<span class="params">*a</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, <span class="built_in">type</span>(a))  <span class="comment"># a = &lt;class &#x27;tuple&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line">my_fn(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p><strong>带星号的形参只能有一个</strong>，带星号的参数，可以和其他参数配合使用，第一个参数给a，剩下的都保存到b的元组中</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 带 * 号的形参只能有一个</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">my_fn</span>(<span class="params">a,*b</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, <span class="built_in">type</span>(a))  <span class="comment"># a = &lt;class &#x27;int&#x27;&gt;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, <span class="built_in">type</span>(b))  <span class="comment"># b = &lt;class &#x27;tuple&#x27;&gt;</span></span><br><span class="line">	<span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, a)  <span class="comment"># a = 1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, b)  <span class="comment"># b = (2, 3, 4)</span></span><br><span class="line">my_fn(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>可变参数不是必须写在最后，但是注意，<strong>带*的参数后的所有参数，必须以关键字参数的形式传递，不然会报错，最好把不定长参数放在最后面</strong></p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 第一个参数给a，剩下的位置参数给b的元组，c必须使用关键字参数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">my_fn</span>(<span class="params">a, *b, c</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, <span class="built_in">type</span>(a))  <span class="comment"># a = &lt;class &#x27;int&#x27;&gt;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, <span class="built_in">type</span>(b))  <span class="comment"># b = &lt;class &#x27;tuple&#x27;&gt;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;c =&quot;</span>, <span class="built_in">type</span>(c))  <span class="comment"># b = &lt;class &#x27;int&#x27;&gt;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, a)  <span class="comment"># a = 1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, b)  <span class="comment"># b = (2, 3)</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;c =&quot;</span>, c)  <span class="comment"># b = 4</span></span><br><span class="line"></span><br><span class="line">my_fn(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, c=<span class="number">4</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>如果在形参的开头直接写一个（ <em>, ）,则要求我们的所有的参数必须以关键字参数的形式传递，如果需要用户在传入实参的时候指定形参的名字，可以在所有参数前面加上一个 （ </em>, ）<strong>, ( *）形参只能接收位置参数，而不能接收关键字参数</strong></p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 如果在形参的开头直接写一个*,则要求我们的所有的参数必须以关键字参数的形式传递</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">my_fn</span>(<span class="params">*, a, b, c</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, <span class="built_in">type</span>(a))  <span class="comment"># a = &lt;class &#x27;int&#x27;&gt;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, <span class="built_in">type</span>(b))  <span class="comment"># b = &lt;class &#x27;int&#x27;&gt;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;c =&quot;</span>, <span class="built_in">type</span>(c))  <span class="comment"># b = &lt;class &#x27;int&#x27;&gt;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, a)  <span class="comment"># a = 1</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, b)  <span class="comment"># b = 2</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;c =&quot;</span>, c)  <span class="comment"># b = 3</span></span><br><span class="line"></span><br><span class="line">my_fn(a=<span class="number">1</span>, b=<span class="number">2</span>, c=<span class="number">3</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>（ <strong> ）形参可以接收其他的关键字参数，它会将这些参数统一保存到一个字典中，字典的key就是参数的名字，字典的value就是参数的值，（ </strong> ）形参只能有一个，并且必须写在所有参数的最后</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">my_fn2</span>(<span class="params">*, a, b, **c</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a =&quot;</span>, a, <span class="string">&quot;type =&quot;</span>, <span class="built_in">type</span>(a))  <span class="comment"># b = 1 type = &lt;class &#x27;int&#x27;&gt;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;b =&quot;</span>, b, <span class="string">&quot;type =&quot;</span>, <span class="built_in">type</span>(b))  <span class="comment"># b = 2 type = &lt;class &#x27;int&#x27;&gt;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;c =&quot;</span>, c, <span class="string">&quot;type =&quot;</span>, <span class="built_in">type</span>(c))  <span class="comment"># c = &#123;&#x27;c&#x27;: 3, &#x27;d&#x27;: 4, &#x27;f&#x27;: 5&#125; type = &lt;class &#x27;dict&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line">my_fn2(a=<span class="number">1</span>, b=<span class="number">2</span>, c=<span class="number">3</span>, d=<span class="number">4</span>, f=<span class="number">5</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>参数的解包（拆包）</p>
<ul>
<li><p>【 <em>元组名 】：通过（ </em> ）来将元组作为参数传入，这里要求序列中元素的个数必须和形参的个数的一致</p>
<p>  元组中元素的个数必须要和函数中形参的个数一样</p>
</li>
<li><p>【 <em>*字典名 】：通过（ </em> ）来将字典作为参数传入，字典中的key必须要和形参的名字对应</p>
<p>  字典中参数的个数必须要和函数中参数的个数一样，且字典中的key必须要和参数对应</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">my_fn3</span>(<span class="params">a, b, c</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a + b + c =&quot;</span>, a + b + c)</span><br><span class="line"></span><br><span class="line"><span class="comment">#使用元组进行参数解包</span></span><br><span class="line">t = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)</span><br><span class="line">my_fn3(*t)</span><br><span class="line"></span><br><span class="line"><span class="comment">#使用字典进行参数解包</span></span><br><span class="line">t = &#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">3</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">10</span>&#125;</span><br><span class="line">my_fn3(**t)</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h2 id="5、返回值"><a href="#5、返回值" class="headerlink" title="5、返回值"></a>5、返回值</h2><ul>
<li>返回值，返回值就是函数执行以后返回的结果，可以通过 return 来指定函数的返回值，可以之间使用函数的返回值，也可以通过一个变量来接收函数的返回值</li>
</ul>
<ul>
<li>return 后边跟什么值，函数就会返回什么值，，return 后边可以跟任意的对象，返回值甚至可以是一个函数</li>
</ul>
<ul>
<li>如果仅仅写一个return 或者 不写return，则相当于return None </li>
</ul>
<ul>
<li><p>在函数中，return后的代码都不会执行，return 一旦执行函数自动结束，<strong>能达到break的效果</strong></p>
<ul>
<li>break：用来退出当前循环</li>
<li>continue：用来跳过当前循环</li>
<li><p>return：用来结束函数，在函数中，也能达到结束循环的效果，return一旦执行，return 之后的语句将不再执行</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">foreach_test</span>(<span class="params">a</span>):</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(a):</span><br><span class="line">        <span class="keyword">if</span> i == <span class="number">5</span>:</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(i, end=<span class="string">&quot; &quot;</span>)  <span class="comment"># 0 1 2 3 4</span></span><br><span class="line"></span><br><span class="line">foreach_test(<span class="number">10</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>函数调用的一个区别</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">test</span>():</span><br><span class="line">    <span class="keyword">return</span></span><br><span class="line"><span class="comment"># test是函数对象，打印test实际是在打印函数对象 &lt;function test at 0x000001EEF74B1D30&gt;</span></span><br><span class="line"><span class="comment"># test()是在调用函数，打印test()实际上是在打印test()函数的返回值 </span></span><br><span class="line"><span class="built_in">print</span>(test)  <span class="comment"># &lt;function test at 0x000001EEF74B1D30&gt;</span></span><br><span class="line"><span class="built_in">print</span>(test())  <span class="comment"># None</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="6、文档字符串"><a href="#6、文档字符串" class="headerlink" title="6、文档字符串"></a>6、文档字符串</h2><ul>
<li><p>help()是Python中的内置函数，通过help()函数可以查询python中的函数的用法</p>
<ul>
<li>语法：help(函数对象)</li>
<li><p>help(print) # 获取print()函数的使用说明</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">help</span>(<span class="built_in">print</span>)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">Help on built-in function print in module builtins:</span></span><br><span class="line"><span class="string">print(...)</span></span><br><span class="line"><span class="string">    print(value, ..., sep=&#x27; &#x27;, end=&#x27;\n&#x27;, file=sys.stdout, flush=False)</span></span><br><span class="line"><span class="string">    </span></span><br><span class="line"><span class="string">    Prints the values to a stream, or to sys.stdout by default.</span></span><br><span class="line"><span class="string">    Optional keyword arguments:</span></span><br><span class="line"><span class="string">    file:  a file-like object (stream); defaults to the current sys.stdout.</span></span><br><span class="line"><span class="string">    sep:   string inserted between values, default a space.</span></span><br><span class="line"><span class="string">    end:   string appended after the last value, default a newline.</span></span><br><span class="line"><span class="string">    flush: whether to forcibly flush the stream.</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>文档字符串（doc str）：文档注释</p>
<ul>
<li>在定义函数时，可以<strong>在函数内部编写文档字符串</strong>，文档字符串就是函数的说明</li>
<li>当我们编写了文档字符串时，就可以通过help()函数来查看函数的说明</li>
<li>文档字符串非常简单，其实直接在函数的第一行写一个字符串就是文档字符串</li>
</ul>
</li>
</ul>
<pre><code>格式：

- 参数后面添加参数类型，只是为了提示该参数需要传入什么类型的值，不是强制要求传入参数类型，只是为了提示用户
- 后面写的返回值类型可以帮助用户了解该函数的返回值类型：
- 在参数后添加参数类型，函数后面添加返回值类型，是为了提示用户，这些类型只是提醒作用，不是强制的作用

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">函数名字</span>(<span class="params">参数<span class="number">1</span>: 参数类型, 参数<span class="number">2</span>: 参数类型, 参数<span class="number">3</span></span>) -&gt; 函数返回值类型:</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">函数的作用，用来求三个参数的和</span></span><br><span class="line"><span class="string">函数的参数：</span></span><br><span class="line"><span class="string">    a：参数1</span></span><br><span class="line"><span class="string">    b：参数2</span></span><br><span class="line"><span class="string">    c：参数3</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">help</span>(my_sum_test)</span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">my_sum_test(a: int, b: str, c) -&gt; str</span></span><br><span class="line"><span class="string">    函数的作用，用来求三个参数的和</span></span><br><span class="line"><span class="string">    函数的参数：</span></span><br><span class="line"><span class="string">        a：参数1</span></span><br><span class="line"><span class="string">        b：参数2</span></span><br><span class="line"><span class="string">        c：参数3</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
</code></pre><h2 id="7、作用域和命名空间"><a href="#7、作用域和命名空间" class="headerlink" title="7、作用域和命名空间"></a>7、作用域和命名空间</h2><p>作用域（scope）：作用域指的是变量生效的区域</p>
<ul>
<li><p>全局作用域（全局变量）：</p>
<ul>
<li><p>全局作用域在程序执行时创建，在程序执行结束时销毁</p>
</li>
<li><p>所有函数以外的区域都是全局作用域</p>
</li>
<li><p>在全局作用域中定义的变量，都属于全局变量，全局变量可以在程序的任意位置被访问</p>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>函数作用域（局部变量）：</p>
<ul>
<li><p>函数作用域在函数调用时创建，在调用结束时销毁</p>
</li>
<li><p>函数每调用一次就会产生一个新的函数作用域</p>
</li>
<li><p>在函数作用域中定义的变量，都是局部变量，它只能在函数内部被访问</p>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>变量的查找：</p>
<ul>
<li>当我们使用变量时，会优先在当前作用域中寻找该变量，如果有则使用，如果没有则继续去上一级作用域中寻找，如果有则使用，如果依然没有则继续去上一级作用域中寻找，以此类推直到找到全局作用域，依然没有找到，则会抛出异常</li>
</ul>
</li>
</ul>
<p>命名空间： </p>
<p>​        <strong>由于命名空间返回的是一个字典，保存的是（变量名字 ：变量值），所以可以使用字典的相关方法来修改变量的值，单一般                    不这样使用</strong></p>
<p>​        命名空间指的是变量存储的位置，</p>
<p>​        每一个变量都需要存储到指定的命名空间当中，</p>
<p>​        每一个作用域都会有一个它对应的命名空间，全局命名空间，用来保存全局变量。</p>
<p>​        函数命名空间用来保存函数中的变量，命名空间实际上就是一个字典，是一个专门用来存储变量的字典</p>
<ul>
<li><p>locals()：</p>
<ul>
<li>用来获取当前作用域的命名空间</li>
<li>如果在全局作用域中调用locals()则获取全局命名空间</li>
<li>如果在函数作用域中调用locals()则获取函数命名空间。</li>
<li><p>locals()返回的是一个字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 命名空间</span></span><br><span class="line">c = <span class="number">100</span></span><br><span class="line"><span class="comment"># 在全局作用域中使用locals</span></span><br><span class="line">a = <span class="built_in">locals</span>()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 函数中使用locals()</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">my_locals</span>():</span><br><span class="line">    c = <span class="number">10</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">locals</span>())</span><br><span class="line"></span><br><span class="line">my_locals()</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>globals()：</p>
<ul>
<li>函数可以用来在任意位置获取全局命名空间</li>
<li>在函数中使用globals()返回的全局作用域，此时，函数中的局部作用域获取不到</li>
</ul>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 命名空间</span></span><br><span class="line">c = <span class="number">100</span></span><br><span class="line"><span class="comment"># 在全局作用域中使用globals</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">globals</span>())</span><br><span class="line"></span><br><span class="line"><span class="comment"># 函数中使用globals()</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">my_locals</span>():</span><br><span class="line">    b = <span class="number">10</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">globals</span>())</span><br><span class="line"></span><br><span class="line">my_locals()</span><br></pre></td></tr></table></figure>
<h2 id="8、递归"><a href="#8、递归" class="headerlink" title="8、递归"></a>8、递归</h2><ul>
<li>递归式的函数<ul>
<li>递归简单理解就是自己去引用自己！</li>
<li>递归式函数，在函数中自己调用自己！</li>
</ul>
</li>
</ul>
<ul>
<li>递归是解决问题的一种方式，它和循环很像，它的整体思想是，将一个大问题分解为一个个的小问题，直到问题无法分解时，再去解决问题</li>
</ul>
<ul>
<li><p>递归式函数的两个要件</p>
<ul>
<li>基线条件：问题可以被分解为的最小问题，当满足基线条件时，递归就不在执行了</li>
<li>递归条件：将问题继续分解的条件</li>
</ul>
</li>
</ul>
<ul>
<li><p>递归和循环类似，基本是可以互相代替的，</p>
<p>  循环编写起来比较容易，阅读起来稍难</p>
<p>  递归编写起来难，但是方便阅读</p>
</li>
</ul>
<h2 id="9、函数式编程"><a href="#9、函数式编程" class="headerlink" title="9、函数式编程"></a>9、函数式编程</h2><ul>
<li>在Python中，函数是一等对象</li>
<li>一等对象一般都会具有如下特点：<ul>
<li>① 对象是在运行时创建的</li>
<li>② 能赋值给变量或作为数据结构中的元素</li>
<li>③ 能作为参数传递</li>
<li>④ 能作为返回值返回</li>
</ul>
</li>
<li><p>高阶函数</p>
<ul>
<li>高阶函数至少要符合以下两个特点中的一个<ul>
<li>① 接收一个或多个函数作为参数</li>
<li>② 将函数作为返回值返回 </li>
</ul>
</li>
</ul>
</li>
<li><p>装饰器</p>
</li>
</ul>
<h2 id="10、高阶函数"><a href="#10、高阶函数" class="headerlink" title="10、高阶函数"></a>10、高阶函数</h2><ul>
<li>和Java中的比较器有点类似，当需要对集合或数组等进行自定义排序的时候，需要闯入一个匿名内部类来实现对集合或数组的自定义操作</li>
</ul>
<ul>
<li><p>高阶函数：接收函数作为参数，或者将函数作为返回值的函数是高阶函数</p>
<p>  当我们使用一个函数作为参数时，实际上是将指定的代码传递进了目标函数</p>
</li>
</ul>
<ul>
<li>当传入的函数参数是一些简单的操作的时候，可以使用lambda表达式来实现。如果是对于一些复杂的操作，这时可以在外面写一个函数保存需要执行的操作，再将该函数传入进去，如下分别写了自定义一个函数参数和使用lambda表达式传入匿名函数的方式来实现高阶函数的操作</li>
</ul>
<pre><code><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 写一个筛选集合为偶数的方法</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">find_even_number</span>(<span class="params">i</span>):</span><br><span class="line">    <span class="keyword">return</span> i % <span class="number">2</span> == <span class="number">0</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#筛选集合的方法</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">filter_even_number</span>(<span class="params">fn, <span class="built_in">list</span></span>):</span><br><span class="line">    new_list = []</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">list</span>:</span><br><span class="line">        <span class="keyword">if</span> fn(i):</span><br><span class="line">            new_list.append(i)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> new_list</span><br><span class="line"></span><br><span class="line"><span class="comment">#将find_even_number函数作为参数传入filter_even_number函数中</span></span><br><span class="line"><span class="built_in">print</span>(filter_even_number(find_even_number, [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>]))  <span class="comment"># [2, 4, 6]</span></span><br></pre></td></tr></table></figure>
</code></pre><ul>
<li><p>匿名函数 lambda 函数表达式 （语法糖）</p>
</li>
<li><p>lambda函数表达式专门用来创建一些简单的函数，他是函数创建的又一种方式</p>
 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">语法：<span class="keyword">lambda</span> 参数列表 : 返回值</span><br></pre></td></tr></table></figure>
</li>
<li><p>匿名函数一般都是作为参数使用，其他地方一般不会使用</p>
</li>
</ul>
<ul>
<li><p>filter()：可以从序列中过滤出符合条件的元素，保存到一个新的序列中</p>
<ul>
<li>参数：<ul>
<li>函数，根据该函数来过滤序列（可迭代的结构）</li>
<li>需要过滤的序列（可迭代的结构）</li>
</ul>
</li>
<li>返回值：</li>
<li>过滤后的新序列（可迭代的结构）</li>
</ul>
</li>
</ul>
<ul>
<li>map()：map()函数可以对可跌倒对象中的所有元素做指定的操作，然后将其添加到一个新的对象中返回</li>
</ul>
<ul>
<li><p>sort()：该方法用来对列表中的元素进行排序、sort()方法默认是直接比较列表中的元素的大小、升序</p>
<ul>
<li>使用sort()对列表进行排序，原有的列表将会被改变，sorted()函数和sort函数用法基本一致，但不会改变列表</li>
<li>该方法需要使用列表或集合 .sort()</li>
<li>参数：在sort()可以接收一个关键字参数 ， key    <ul>
<li>对于列表中元素类型不一样的话，不传入参数指定什么排列的话，输出列表会和没有排序之前基本一样</li>
<li>key需要一个函数作为参数，当设置了函数作为参数，每次都会以列表中的一个元素作为参数来调用函数，并且使用函数的返回值来比较元素的大小</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li>sorted()：这个函数和sort()的用法基本一致，但是sorted()可以对任意的序列进行排序，并且使用sorted()排序不会影响原来的对象，而是返回一个新对象</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 语法：lambda 参数列表 : 返回值</span></span><br><span class="line"></span><br><span class="line">my_list = [<span class="number">3</span>, <span class="number">5</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">6</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># filter()：使用lambda表达式传入一个筛选集合中偶数的方法</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">list</span>(<span class="built_in">filter</span>(<span class="keyword">lambda</span> i: i % <span class="number">2</span> == <span class="number">0</span>, my_list)))  <span class="comment"># [2, 4, 6]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># map()：可以对集合中的每一个元素做指定的操作：</span></span><br><span class="line"><span class="comment"># 将集合中的每一个元素加 1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">list</span>(<span class="built_in">map</span>(<span class="keyword">lambda</span> i: i + <span class="number">1</span>, my_list)))  <span class="comment"># [2, 3, 4, 5, 6, 7]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># sort()：该方法用来对列表中的元素进行排序、sort()方法默认是直接比较列表中的元素的大小、</span></span><br><span class="line"><span class="comment"># 使用默认的升序排序，对于int类型的列表，对于混合类型的列表，会直接输出原列表</span></span><br><span class="line"></span><br><span class="line">my_list = [<span class="number">3</span>, <span class="string">&quot;5&quot;</span>, <span class="number">1</span>, <span class="string">&quot;4&quot;</span>, <span class="number">2</span>, <span class="number">6</span>]</span><br><span class="line"><span class="built_in">print</span>(my_list)  <span class="comment"># [3, &#x27;5&#x27;, 1, &#x27;4&#x27;, 2, 6]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># key 中传入什么类型，sort会将集合中的元素全部转换为传入的类型，对于不能转换的类型会报错</span></span><br><span class="line">my_list.sort(key=<span class="built_in">str</span>)</span><br><span class="line"><span class="built_in">print</span>(my_list)  <span class="comment"># [1, 2, 3, &#x27;4&#x27;, &#x27;5&#x27;, 6]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># sorted()：sort()的用法基本一致，可以对任意的序列进行排序，不会影响原来的对象</span></span><br><span class="line"><span class="comment"># 排序前</span></span><br><span class="line">my_list = [<span class="number">3</span>, <span class="string">&quot;5&quot;</span>, <span class="number">1</span>, <span class="string">&quot;4&quot;</span>, <span class="number">2</span>, <span class="number">6</span>]</span><br><span class="line"><span class="built_in">print</span>(my_list)  <span class="comment"># [3, &#x27;5&#x27;, 1, &#x27;4&#x27;, 2, 6]</span></span><br><span class="line"><span class="comment"># 排序后</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">sorted</span>(my_list, key=<span class="built_in">int</span>))  <span class="comment"># [1, 2, 3, &#x27;4&#x27;, &#x27;5&#x27;, 6]</span></span><br><span class="line"><span class="comment"># 排序后测试原来的列表是否修改</span></span><br><span class="line"><span class="built_in">print</span>(my_list)  <span class="comment"># [3, &#x27;5&#x27;, 1, &#x27;4&#x27;, 2, 6]</span></span><br></pre></td></tr></table></figure>
<h2 id="11、闭包函数"><a href="#11、闭包函数" class="headerlink" title="11、闭包函数"></a>11、闭包函数</h2><p>将函数作为返回值返回，也是一种高阶函数，可以将一些私有的数据藏到的闭包中，只有当接收到了返回值的时候才能访问一些变量</p>
<p>在函数内部定义内部函数将内部函数返回，这时在外部可以使用一个变量接收函数返回的内部函数，该返回值可以范围函数中定义的变量，因为返回的是函数的内部函数，作用域是由内到外的访问</p>
<ul>
<li>形成闭包的要件<ul>
<li>函数嵌套</li>
<li>将内部函数作为返回值返回</li>
<li>内部函数必须要使用到外部函数的变量</li>
</ul>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 闭包函数的使用</span></span><br><span class="line"><span class="comment"># sum()用来求一个列表中所有元素的和</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">average</span>():</span><br><span class="line">    num = []</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">average2</span>(<span class="params">n</span>):</span><br><span class="line">        num.append(n)</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">sum</span>(num) / <span class="built_in">len</span>(num)</span><br><span class="line">	<span class="comment">#将内部函数返回</span></span><br><span class="line">    <span class="keyword">return</span> average2</span><br><span class="line"></span><br><span class="line"><span class="comment">#average函数返回的是一个内部函数，average函数的返回值可以访问到average函数中的变量</span></span><br><span class="line">method = average()</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(method(<span class="number">10</span>))  <span class="comment"># 10.0</span></span><br><span class="line"><span class="built_in">print</span>(method(<span class="number">15</span>))  <span class="comment"># 12.5</span></span><br></pre></td></tr></table></figure>
<h2 id="12、装饰器（生成代理类）"><a href="#12、装饰器（生成代理类）" class="headerlink" title="12、装饰器（生成代理类）"></a>12、装饰器（生成代理类）</h2><p>和Java中的动态代理类实效果差不多，需要在不修改源代码的前提下在原有的函数下添加新的功能</p>
<p>以下的静态代理、动态代理、使用注解都是我通过学习Java后为了更好理解python中的装饰器写的下·</p>
<h3 id="静态代理"><a href="#静态代理" class="headerlink" title="静态代理"></a>静态代理</h3><p>只能为一个函数使用</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">a, b</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    求任意两个数的和</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;这里是add函数的内部&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> a + b</span><br><span class="line"></span><br><span class="line"><span class="comment"># 在没有修改add(a, b)函数的前提下给add(a, b)函数添加了新的功能</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">static_proxy</span>(<span class="params">a, b</span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;前置通知&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">    result = add(a, b)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;后置通知&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(static_proxy(<span class="number">20</span>, <span class="number">20</span>), end=<span class="string">&quot; &quot;</span>)  <span class="comment"># 前置通知 这里是add函数的内部 后置通知 add函数的返回值 = 40 </span></span><br></pre></td></tr></table></figure>
<h3 id="动态代理"><a href="#动态代理" class="headerlink" title="动态代理"></a>动态代理</h3><p>可以给不同的函数使用，随着传入的函数分生成不同函数的装饰器，装饰器的决定由调用者决定</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">a, b</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    求任意两个数的和</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;这里是add函数的内部&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> a + b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">mul</span>(<span class="params">a, b</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    求任意两个数的乘积</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;这里是mul函数的内部&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> a * b</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">proxy_function</span>(<span class="params">old</span>):</span><br><span class="line">    <span class="comment"># 像Java中的代理类对象</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">proxy</span>(<span class="params">*a, **b</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;前置通知&quot;</span>)</span><br><span class="line">        result = old(*a, **b)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;后置通知&quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> proxy</span><br><span class="line"></span><br><span class="line">function_add = proxy_function(add)</span><br><span class="line"><span class="comment"># 如下语句的输出结果： 前置通知 这里是add函数的内部 后置通知 函数对象的返回值为 30</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;函数对象的返回值为&quot;</span>, function_add(<span class="number">10</span>, <span class="number">20</span>))  </span><br><span class="line"></span><br><span class="line">function_mul = proxy_function(mul)</span><br><span class="line"><span class="comment"># 如下语句的输出结果： 前置通知 这里是mul函数的内部 后置通知 函数对象的返回值为 200</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;函数对象的返回值为&quot;</span>, function_mul(<span class="number">10</span>, <span class="number">20</span>))  </span><br></pre></td></tr></table></figure>
<h3 id="使用注解"><a href="#使用注解" class="headerlink" title="使用注解"></a>使用注解</h3><p>使用注解会修改函数原来执行结果，但是不会修改函数的源代码，想要原来的执行结果只需要将注解删除即可</p>
<p>使用方法：@装饰器（装饰器是一个函数，使用：@函数名）</p>
<ul>
<li>使用注解添加多个装饰器执行的先后顺序：<ul>
<li>为一个函数指定多个装饰器，这样函数将会安装从内向外的顺序被装饰 </li>
<li>谁的装饰器在前面，就谁的前置通知先执行，前置通知的顺序是装饰器在前面就先执行</li>
<li>谁的装饰器在前面，谁的后置通知就最后执行，后置通知的顺序是装饰器在前面就后执行</li>
</ul>
</li>
</ul>
<ul>
<li><p>使用一个装饰器</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">proxy_function</span>(<span class="params">old</span>):</span><br><span class="line">    <span class="comment"># 像Java中的代理类对象</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">proxy</span>(<span class="params">*a, **b</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;前置通知&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">        result = old(*a, **b)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;后置通知&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> proxy</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@proxy_function</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">delete</span>(<span class="params">a, b</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    求任意两个数相减</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;这里是delete函数的内部&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> a - b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(delete(<span class="number">15</span>, <span class="number">10</span>))  <span class="comment"># 前置通知 这里是delete函数的内部 后置通知 5</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>使用两个及以上装饰器</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">proxy_function</span>(<span class="params">old</span>):</span><br><span class="line">    <span class="comment"># 像Java中的代理类对象</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">proxy</span>(<span class="params">*a, **b</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;前置通知&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">        result = old(*a, **b)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;后置通知&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> proxy</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">proxy_function2</span>(<span class="params">old</span>):</span><br><span class="line">    <span class="comment"># 像Java中的代理类对象</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">proxy2</span>(<span class="params">*a, **b</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;前置通知2&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">        result = old(*a, **b)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;后置通知2&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> proxy2</span><br><span class="line"></span><br><span class="line"><span class="comment"># 给函数添加两个装饰器</span></span><br><span class="line"><span class="meta">@proxy_function</span></span><br><span class="line"><span class="meta">@proxy_function2</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">delete</span>(<span class="params">a, b</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    求任意两个数相减</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;这里是delete函数的内部&quot;</span>, end=<span class="string">&quot; &quot;</span>)</span><br><span class="line">    <span class="keyword">return</span> a - b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(delete(<span class="number">15</span>, <span class="number">10</span>))  <span class="comment"># 前置通知 前置通知2 这里是delete函数的内部 后置通知2 后置通知 5</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="六、面向对象"><a href="#六、面向对象" class="headerlink" title="六、面向对象"></a>六、面向对象</h1><h2 id="1、什么是对象"><a href="#1、什么是对象" class="headerlink" title="1、什么是对象"></a>1、什么是对象</h2><ul>
<li>对象是内存中专门用来存储数据的一块区域。</li>
<li>对象中可以存放各种数据（比如：数字、布尔值、代码）</li>
<li>对象由三部分组成：<ul>
<li>对象的标识（id）</li>
<li>对象的类型（type）</li>
<li>对象的值（value）</li>
</ul>
</li>
</ul>
<h2 id="2、面向对象"><a href="#2、面向对象" class="headerlink" title="2、面向对象"></a>2、面向对象</h2><ul>
<li><p>Python是一门面向对象的编程语言</p>
</li>
<li><p>所谓的面向对象的语言，简单理解就是语言中的所有操作都是通过对象来进行的</p>
</li>
</ul>
<ul>
<li><p>面向过程的编程的语言</p>
<ul>
<li>面向过程指将我们的程序的逻辑分解为一个一个的步骤，通过对每个步骤的抽象，来完成程序</li>
<li>例子：<ul>
<li>孩子上学</li>
<li>妈妈起床</li>
<li>妈妈上厕所</li>
<li>妈妈洗漱</li>
<li>妈妈做早饭</li>
<li>妈妈叫孩子起床</li>
<li>孩子上厕所</li>
<li>孩子要洗漱</li>
<li>孩子吃饭</li>
<li>孩子背着书包上学校</li>
</ul>
</li>
</ul>
</li>
<li><p>面向过程的编程思想将一个功能分解为一个一个小的步骤，我们通过完成一个一个的小的步骤来完成一个程序</p>
<ul>
<li>这种编程方式，符合我们人类的思维，编写起来相对比较简单</li>
<li>但是这种方式编写代码的往往只适用于一个功能，如果要在实现别的功能，即使功能相差极小，也往往要重新编写代码，所以它可复用性比较低，并且难于维护 </li>
</ul>
</li>
<li><p>面向对象的编程语言</p>
<ul>
<li>面向对象的编程语言，关注的是对象，而不关注过程 </li>
<li>对于面向对象的语言来说，一切都是对象       </li>
<li>例子：<ul>
<li>孩他妈起床叫孩子上学</li>
</ul>
</li>
<li>面向对象的编程思想，将所有的功能统一保存到对应的对象中，比如，妈妈功能保存到妈妈的对象中，孩子的功能保存到孩子对象中，要使用某个功能，直接找到对应的对象即可</li>
<li>这种方式编写的代码，比较容易阅读，并且比较易于维护，容易复用。</li>
<li>但是这种方式编写，不太符合常规的思维，编写起来稍微麻烦一点 </li>
</ul>
</li>
</ul>
<ul>
<li>简单归纳一下，面向对象的思想<ul>
<li>找对象</li>
<li>搞对象</li>
</ul>
</li>
</ul>
<h2 id="3、类"><a href="#3、类" class="headerlink" title="3、类"></a>3、类</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">我们目前所学习的对象都是Python内置的对象</span><br><span class="line"></span><br><span class="line">但是内置对象并不能满足所有的需求，所以我们在开发中经常需要自定义一些对象</span><br><span class="line"></span><br><span class="line">类，简单理解它就相当于一个图纸。在程序中我们需要根据类来创建对象</span><br><span class="line"></span><br><span class="line">类就是对象的图纸！</span><br><span class="line"></span><br><span class="line">我们也称对象是类的实例（instance）</span><br><span class="line"></span><br><span class="line">如果多个对象是通过一个类创建的，我们称这些对象是一类对象</span><br><span class="line"></span><br><span class="line">像 <span class="built_in">int</span>() <span class="built_in">float</span>() <span class="built_in">bool</span>() <span class="built_in">str</span>() <span class="built_in">list</span>() <span class="built_in">dict</span>() .... 这些都是类</span><br><span class="line"></span><br><span class="line">a = <span class="built_in">int</span>(<span class="number">10</span>) <span class="comment"># 创建一个int类的实例 等价于 a = 10</span></span><br><span class="line"></span><br><span class="line">我们自定义的类都需要使用大写字母开头，使用大驼峰命名法（帕斯卡命名法）来对类命名</span><br><span class="line"></span><br><span class="line">类也是一个对象！</span><br><span class="line"></span><br><span class="line">类就是一个用来创建对象的对象！</span><br><span class="line"></span><br><span class="line">类是<span class="built_in">type</span>类型的对象，定义类实际上就是定义了一个<span class="built_in">type</span>类型的对象</span><br></pre></td></tr></table></figure>
<ul>
<li><p>定义一个简单的类</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个简单的类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyClass</span>:</span><br><span class="line">    <span class="keyword">pass</span>  <span class="comment"># 使用pass占位符</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(MyClass)  <span class="comment"># &lt;class &#x27;__main__.MyClass&#x27;&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>使用类创建一个对象</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">MyClass</span>:</span><br><span class="line">    <span class="keyword">pass</span>  <span class="comment"># 使用pass占位符</span></span><br><span class="line"></span><br><span class="line">mc = MyClass()</span><br><span class="line"><span class="built_in">print</span>(mc)<span class="comment">#&lt;__main__.MyClass object at 0x00000187AB4AF5E0&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>isinstance()用来检查一个对象是否是一个类的实例</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个简单的类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyClass</span>:</span><br><span class="line">    <span class="keyword">pass</span>  <span class="comment"># 使用pass占位符</span></span><br><span class="line"></span><br><span class="line">mc = MyClass()</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">isinstance</span>(mc, MyClass))  <span class="comment"># True</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">isinstance</span>(mc, <span class="built_in">str</span>))  <span class="comment"># False</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>可以向对象中添加变量，对象中的变量称为属性（不推荐使用，了解即可）</p>
<ul>
<li><p>语法：对象.属性名 = 属性值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个简单的类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyClass</span>:</span><br><span class="line">    <span class="keyword">pass</span>  <span class="comment"># 使用pass占位符</span></span><br><span class="line"></span><br><span class="line">mc = MyClass()</span><br><span class="line">mc.name = <span class="string">&quot;李白&quot;</span></span><br><span class="line"><span class="built_in">print</span>(mc.name)  <span class="comment"># 李白</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h3 id="类创建对象的流程"><a href="#类创建对象的流程" class="headerlink" title="类创建对象的流程"></a>类创建对象的流程</h3><p><img src= "" data-lazy-src="https://www.naste.top:9000/webp/image-20210314101209943.webp" alt="image-20210314101209943"></p>
<h3 id="类的定义"><a href="#类的定义" class="headerlink" title="类的定义"></a>类的定义</h3><ul>
<li><p>类和对象都是对现实生活中的事物或程序中的内容的抽象</p>
</li>
<li><p>实际上所有的事物都由两部分构成：</p>
<ul>
<li>数据（属性）</li>
</ul>
</li>
<li><p>.行为（方法）</p>
</li>
<li><p>在类的代码块中，我们可以定义变量和函数，变量会成为该类实例的公共属性，所有的该类实例都可以通过 对象.属性名 的形式访问 ，函数会成为该类实例的公共方法，所有该类实例都可以通过 对象.方法名() 的形式调用方法</p>
</li>
<li><p>注意：<strong>方法调用时，第一个参数由解析器自动传递，所以定义方法时，至少要定义一个形参！</strong> </p>
</li>
<li><p>实例为什么能访问到类中的属性和方法<br>  类中定义的属性和方法都是公共的，任何该类实例都可以访问</p>
<ul>
<li><p>属性和方法查找的流程</p>
<p>  当我们调用一个对象的属性时，解析器会先在当前对象中寻找是否含有该属性，</p>
<ul>
<li>如果有，则直接返回当前的对象的属性值，</li>
</ul>
</li>
</ul>
</li>
<li><p>如果没有，则去当前对象的类对象中去寻找，如果有则返回类对象的属性值</p>
<pre><code>  -  如果类对象中依然没有，则报错！
</code></pre><ul>
<li><p>类对象和实例对象中都可以保存属性（方法）</p>
<ul>
<li>如果这个属性（方法）是所有的实例共享的，则应该将其保存到类对象中</li>
<li>如果这个属性（方法）是某个实例独有，则应该保存到实例对象中     </li>
</ul>
</li>
<li><p>一般情况下，属性保存到实例对象中，而方法需要保存到类对象中</p>
</li>
</ul>
</li>
</ul>
<p><img src= "" data-lazy-src="https://www.naste.top:9000/webp/image-20210314102006600.webp" alt="image-20210314102006600"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个表示人的类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>:</span><br><span class="line">    <span class="comment"># 在代码块中可以定义变量和函数</span></span><br><span class="line">    <span class="comment"># 在类中定义的变量，两会称为所有的实例的公共属性</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 定义一个公共属性，所有示例都可以访问到</span></span><br><span class="line">    name = <span class="string">&quot;李白&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 定义一个函数，类中定义的函数称为方法，这些方法可以通过类的实例来访问</span></span><br><span class="line">    <span class="comment"># 方法每次被调用时，解析器都会自动传递第一个实参</span></span><br><span class="line">    <span class="comment"># 第一个参数，就是调用方法的对象本身，</span></span><br><span class="line">    <span class="comment"># 如果是p1调的，则第一个参数就是p1对象</span></span><br><span class="line">    <span class="comment"># 如果是p2调的，则第一个参数就是p2对象</span></span><br><span class="line">    <span class="comment"># 一般我们都会将这个参数命名为self</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">say_hello</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;你好&quot;</span>)</span><br><span class="line"></span><br><span class="line">person = Person()</span><br><span class="line"><span class="comment"># 调用属性</span></span><br><span class="line"><span class="built_in">print</span>(person.name)  <span class="comment"># 李白</span></span><br><span class="line"><span class="comment"># 调用方法</span></span><br><span class="line"><span class="comment"># 方法调用和函数调用的区别</span></span><br><span class="line"><span class="comment"># 如果是函数调用，则调用时传几个参数，就会有几个实参</span></span><br><span class="line"><span class="comment"># 如果是方法调用，默认传递一个参数，所以方法中至少要定义一个形参</span></span><br><span class="line"><span class="comment"># 第一个参数由解析器自动传递</span></span><br><span class="line">person.say_hello()  <span class="comment"># 你好</span></span><br></pre></td></tr></table></figure>
<h2 id="4、对象初始化"><a href="#4、对象初始化" class="headerlink" title="4、对象初始化"></a>4、对象初始化</h2><ul>
<li>p1 = Person()的运行流程<ul>
<li>创建一个变量</li>
<li>在内存中创建一个新对象</li>
<li>（ <strong> init </strong> ）(self)方法执行，和Java中的构造函数类似，只要对象一创建该方法就会被执行</li>
<li>将对象的id赋值给变量</li>
</ul>
</li>
</ul>
<h2 id="5、1-5总结"><a href="#5、1-5总结" class="headerlink" title="5、1-5总结"></a>5、1-5总结</h2><p>以下总结对比Java来理解的</p>
<ul>
<li><p>类的基本结构</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">类名</span>([父类]):</span><br><span class="line">    公共的属性...</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 对象的初始化方法，类似Java中的构造函数</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, ...</span>):</span><br><span class="line">        ...</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 其他的方法    </span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">method_1</span>(<span class="params">self, ...</span>):</span><br><span class="line">        ...</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">method_2</span>(<span class="params">self, ...</span>):</span><br><span class="line">        ...</span><br><span class="line"></span><br><span class="line">    ...</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>属性：</p>
<ul>
<li>在Java中定义公共属性，这个属性是属于每个对象所独有的，Java中定义属性可以不赋初始值</li>
<li>属性<ul>
<li>在python中，对于一个类所共有的属性可以定义在类的内部</li>
<li>以下情况为定义一个Person类来解释的<ul>
<li>一般可以直接定义在类内部的属性有：<ul>
<li>比如一个人有两条腿可以定义一个属性为（leg = 2）</li>
</ul>
</li>
<li>一般不可以在内部的属性有，对于一些属性是不一样的却又是共有的属性，只是属性值不一样<ul>
<li>比如每个人的名字都不一样，所以一般不在类的内部定义人的名字这个属性</li>
<li>这时可以添加定义一些特殊方法<ul>
<li>特殊方法格式：以（ <strong> 开头，</strong> ）结尾的方法，<strong>两个下划线开头的函数是声明该属性为私有</strong></li>
<li>特殊方法不需要我们自己调用，特殊方法将会在特殊的时刻自动调用</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>方法：</p>
<ul>
<li>在Java中方法可以没有参数</li>
<li>在python中只要定义了方法一定要定义一个形参，方法在被调用的时候不需要往该方法中注入实参，这个参数是由解析器自动传入的，传入每个对象的id，用于区分每个对象</li>
</ul>
</li>
</ul>
<ul>
<li><p><em> init</em>方法 ：和Java中的构造函数差不多，在 <em> init </em>方法中有一个self形参，系统会自动将创建的对象的id的值传入进去</p>
<ul>
<li>在Java中可以定义多个构造函数，有参构造和无参构造</li>
<li>在python中只能定义一个init方法</li>
</ul>
</li>
</ul>
<ul>
<li><p>python方法的self参数</p>
<ul>
<li><p><em> init </em>方法中的self参数</p>
<ul>
<li>在init方法中可以使用self参数定义属性给属性赋值，这时候定义的属性和值由于是self参数定义的，所以这个参数的属性和值可以属于不同的属性</li>
<li><strong>init方法中定义的属性可以在类外面定义的对象通过（对象.属性名）调用</strong></li>
</ul>
</li>
</ul>
</li>
</ul>
<pre><code>- 类中其他方法中的self参数

    - init以外的方法使用self参数也可以定义属性，但和init方法中有区别，在除init方法使用self参数定义的属性来定义的属性，除了该方法内部可以使用定义的属性，其他方法不能访问到，类外面定义对象也不能调用，**所以在除了init方法的其他方法通过self参数定义属性的属性都是没有意义的**
</code></pre><h2 id="6、封装"><a href="#6、封装" class="headerlink" title="6、封装"></a>6、封装</h2><h3 id="封装1"><a href="#封装1" class="headerlink" title="封装1"></a>封装1</h3><p>6.1 — 6.5中定义的属性和方法相当于Java中的public，是公开的</p>
<p>下面的封装实现的是要将一些不希望被外界访问的方法和属性封装起来，设置这些属性和方法的访问权限</p>
<p>封装是面向对象的三大特性</p>
<p>封装是指隐藏对象中一些不希望被外部所访问到的属性或方法</p>
<ul>
<li>如何隐藏一个对象中的属性？</li>
<li>如何获取（修改）对象中的属性？<ul>
<li>需要提供一个getter和setter方法使外部可以访问到属性    <ul>
<li>getter 获取对象中的指定属性（get_属性名）</li>
<li>setter 用来设置对象的指定属性（set_属性名）</li>
</ul>
</li>
</ul>
</li>
<li>使用封装，确实增加了类的定义的复杂程度，但是它也确保了数据的安全性<ul>
<li>隐藏了属性名，使调用者无法随意的修改对象中的属性</li>
<li>增加了getter和setter方法，很好的控制的属性是否是只读的<ul>
<li>如果希望属性是只读的，则可以直接去掉setter方法</li>
<li>如果希望属性不能被外部访问，则可以直接去掉getter方法</li>
</ul>
</li>
<li>使用setter方法设置属性，可以增加数据的验证，确保数据的值是正确的</li>
<li>使用getter方法获取属性，使用setter方法设置属性<ul>
<li>可以在读取属性和修改属性的同时做一些其他的处理</li>
</ul>
</li>
<li>使用getter方法可以表示一些计算的属性</li>
</ul>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用定义的属性名和init方法的形参名不一样的方法来创建属性，达到封装的一部分效果</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name</span>):</span><br><span class="line">        self.hidden_name = name</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">person_print</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(self.hidden_name)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">set_name</span>(<span class="params">self, name</span>):</span><br><span class="line">        self.hidden_name = name</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">get_name</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> self.hidden_name</span><br><span class="line"></span><br><span class="line">person = Person(<span class="string">&quot;韩信&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(person.get_name())</span><br></pre></td></tr></table></figure>
<h3 id="封装2"><a href="#封装2" class="headerlink" title="封装2"></a>封装2</h3><p>封装1中解决属性的问题虽然得到了部分解决，但是要是调用者知道了开发者内部自定义的属性名字。调用者还是能修改其代码。</p>
<ul>
<li><p>可以为对象的属性使用双下划线开头，__xxx</p>
</li>
<li><p>双下划线开头的属性，是对象的隐藏属性，隐藏属性只能在类的内部访问，无法通过对象访问</p>
</li>
<li><p>其实隐藏属性只不过是Python自动为属性改了一个名字</p>
<ul>
<li><strong>实际上是将名字修改为了，<em>类名 _</em> 属性名</strong> 比如 <strong> name -&gt; _Person</strong>name</li>
</ul>
</li>
<li><p>使用__开头的属性，实际上依然可以在外部访问，所以这种方式我们一般不用</p>
</li>
<li><p>一般我们会将一些私有属性（不希望被外部访问的属性）以_开头</p>
</li>
<li><p>一般情况下，使用_开头的属性都是私有属性，没有特殊需要不要修改私有属性，只是设提示作用</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name</span>):</span><br><span class="line">        self.__name = name</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">person_print</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(self.__name)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">set_name</span>(<span class="params">self, name</span>):</span><br><span class="line">        self.__name = name</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">get_name</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> self.__name</span><br><span class="line"></span><br><span class="line">person = Person(<span class="string">&quot;韩信&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(person._Person__name)  <span class="comment"># 韩信</span></span><br><span class="line"><span class="built_in">print</span>(person.get_name())  <span class="comment"># 韩信</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="封装3"><a href="#封装3" class="headerlink" title="封装3"></a>封装3</h3><ul>
<li><p>使用property装饰器可以为属性生成get方法，在创建对象调用属性的时候直接（ 对象名.属性名 ）就会调用对应的get方法</p>
</li>
<li><p>property装饰器，用来将一个get方法，转换为对象的属性</p>
<ul>
<li>添加为property装饰器以后，我们就可以像调用属性一样使用get方法</li>
<li>使用property装饰的方法，必须和属性名是一样的</li>
</ul>
</li>
<li><p>setter方法的装饰器：@属性名.setter</p>
<ul>
<li>使用setter装饰器可以为属性生成对应set方法，用创建的对象给属性修改值的时候直接（ 对象名.属性名 = 值 ）就可以修改属性的值了</li>
<li><p><strong>注意：在属性使用setter装饰器之前需要对属性使用property装饰器，不然setter装饰器不能使用</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name</span>):</span><br><span class="line">        self.__name = name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">name</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> self.__name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @name.setter</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">name</span>(<span class="params">self, name</span>):</span><br><span class="line">        self.__name = name</span><br><span class="line"></span><br><span class="line">person = Person(<span class="string">&quot;韩信&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(person.name)  <span class="comment"># 韩信</span></span><br><span class="line"></span><br><span class="line">person.name = <span class="string">&quot;李白&quot;</span></span><br><span class="line"><span class="built_in">print</span>(person.name)  <span class="comment"># 李白</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<h2 id="7、继承"><a href="#7、继承" class="headerlink" title="7、继承"></a>7、继承</h2><ul>
<li><p>有一个类，能够实现我们需要的大部分功能，但是不能实现全部功能，如何能让这个类来实现全部的功能呢？</p>
<ul>
<li><p>直接修改这个类，在这个类中添加我们需要的功能</p>
<ul>
<li>修改起来会比较麻烦，并且会违反OCP原则</li>
</ul>
</li>
<li><p>直接创建一个新的类</p>
<ul>
<li>创建一个新的类比较麻烦，并且需要大量的进行复制粘贴，会出现大量的重复性代码</li>
</ul>
</li>
<li><p>直接从Animal类中来继承它的属性和方法</p>
<ul>
<li><p>继承是面向对象三大特性之一</p>
</li>
<li><p>通过继承我们可以使一个类获取到其他类中的属性和方法</p>
</li>
<li><p>定义类时，可以在类名后的括号中指定当前类的父类（超类、基类、super）</p>
</li>
<li><p>子类（衍生类）可以直接继承父类中的所有的属性和方法</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li>通过继承可以直接让子类获取到父类的方法或属性，避免编写重复性的代码，并且也符合OCP原则</li>
</ul>
<ul>
<li>所以我们经常需要通过继承来对一个类进行扩展</li>
</ul>
<ul>
<li><strong>在创建类时，如果省略了父类，则默认父类为object，object是所有类的父类，所有类都继承自object</strong></li>
</ul>
<ul>
<li><p>issubclass()：检查一个类是否是另一个类的子类</p>
<ul>
<li>issubclass(类A, 类B)：如果类A为类B的子类返回True，否则返回False</li>
</ul>
</li>
</ul>
<ul>
<li>所有对象都是Object的实例</li>
</ul>
<ul>
<li><p>父类中的所有方法都会被子类继承，包括特殊方法，也可以重写特殊方法（从最开始的object类下来，只要是往上的类都会被继承）</p>
</li>
<li><p>super() 可以用来获取当前类的父类，并且通过super()返回对象调用父类方法时，不需要传递self，但需要传递父类init方法中的其他参数</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Animal</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, <span class="built_in">id</span></span>):</span><br><span class="line">        self._<span class="built_in">id</span> = <span class="built_in">id</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">say</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;动物会叫&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 写一个狗类继承动物类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span>(<span class="title class_ inherited__">Animal</span>):</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, age, <span class="built_in">id</span></span>):</span><br><span class="line">        <span class="built_in">super</span>().__init__(<span class="built_in">id</span>)</span><br><span class="line">        self._age = age</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">say</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;狗：汪汪汪&quot;</span>)</span><br><span class="line">        </span><br><span class="line">blackDog = Black_Dog(<span class="number">19</span>, <span class="number">1</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="重写"><a href="#重写" class="headerlink" title="重写"></a>重写</h3><ul>
<li>如果在子类中如果有和父类同名的方法，则通过子类实例去调用方法时，会调用子类的方法而不是父类的方法，这个特点我们成为叫做方法的重写（覆盖，override）</li>
</ul>
<p>当我们调用一个对象的方法时，会优先去当前对象中寻找是否具有该方法，如果有则直接调用，</p>
<p>如果没有，则去当前对象的父类中寻找，如果父类中有则直接调用父类中的方法，</p>
<p> 如果没有，则去父类的父类中寻找，以此类推，直到找到object，如果依然没有找到，则报错</p>
<h2 id="8、多重继承-了解下"><a href="#8、多重继承-了解下" class="headerlink" title="8、多重继承(了解下)"></a>8、多重继承(了解下)</h2><p>一个子类继承多个父类</p>
<ul>
<li><p>在Python中是支持多重继承的，也就是我们可以为一个类同时指定多个父类</p>
</li>
<li><p>可以在类名的()后边添加多个类，来实现多重继承</p>
</li>
<li><p>多重继承，会使子类同时拥有多个父类，并且会获取到所有父类中的方法</p>
</li>
<li><p><strong>在开发中没有特殊的情况，应该尽量避免使用多重继承，因为多重继承会让我们的代码过于复杂</strong></p>
</li>
<li><p>如果多个父类中有同名的方法，则会现在第一个父类中寻找，然后找第二个，然后找第三个。。。</p>
</li>
<li><p>前边父类的方法会覆盖后边父类的方法</p>
<p>  <img src= "" data-lazy-src="https://www.naste.top:9000/webp/image-20210314144017994.webp" alt="image-20210314144017994"></p>
</li>
</ul>
<h2 id="9、多态"><a href="#9、多态" class="headerlink" title="9、多态"></a>9、多态</h2><p>多态是面向对象的三大特征之一，多态从字面上理解是多种形态，一个对象可以以不同的形态去呈现</p>
<p>当由一个方法，脸面需要传入一个类，这个形参需要调用里面的一个属性进行输出，这时使用多态，只要传入的参数的类中有该属性，该方法里面就能输出属性</p>
<p>和Java中的反射有点类似，对于一个方法传入的是哪个类就执行哪个类中的方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">A</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        self._<span class="built_in">id</span> = <span class="number">11</span></span><br><span class="line"></span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">id</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> self._<span class="built_in">id</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        self._<span class="built_in">id</span> = <span class="number">12</span></span><br><span class="line">        </span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">id</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> self._<span class="built_in">id</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">test</span>(<span class="params">obj</span>):</span><br><span class="line">    <span class="built_in">print</span>(obj.<span class="built_in">id</span>)</span><br><span class="line"></span><br><span class="line">test(A())  <span class="comment"># 11</span></span><br><span class="line">test(B())  <span class="comment"># 12</span></span><br></pre></td></tr></table></figure>
<h2 id="10、类中的属性和方法"><a href="#10、类中的属性和方法" class="headerlink" title="10、类中的属性和方法"></a>10、类中的属性和方法</h2><ul>
<li>属性：<ul>
<li>类属性：<ul>
<li>直接在类中定义的属性是类属性（一般不适用，除非有一些公共的属性，值一样的属性）</li>
<li>类属性可以通过类或类的实例访问到</li>
<li>但是类属性只能通过类对象来修改，无法通过实例对象修改</li>
</ul>
</li>
<li>实例属性：<ul>
<li>通过实例对象添加的属性属于实例属性</li>
<li>实例属性只能通过实例对象来访问和修改，类对象无法访问修改</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>方法</p>
<ul>
<li>实例方法：<ul>
<li>在类中定义，以self为第一个参数的方法都是实例方法</li>
<li>实例方法在调用时，Python会将调用对象作为self传入  </li>
<li>实例方法可以通过实例和类去调用<ul>
<li>当通过实例调用时，会自动将当前调用对象作为self传入</li>
<li>当通过类调用时，不会自动传递self，此时我们必须手动传递self</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<pre><code>- 类方法：
    - 在类内部使用 @classmethod 来修饰的方法属于类方法
    - 类方法的第一个参数是cls，也会被自动传递，cls就是当前的类对象
        - 类方法和实例方法的区别，实例方法的第一个参数是self，而类方法的第一个参数是cls
        - **类方法可以通过类去调用，也可以通过实例调用，**没有区别



- 静态方法：
    - 在类中使用 @staticmethod 来修饰的方法属于静态方法  
    - 静态方法不需要指定任何的默认参数，静态方法可以通过类和实例去调用  
    - 静态方法，基本上是一个和当前类无关的方法，它只是一个保存到当前类中的函数
    - 静态方法一般都是一些工具方法，和当前类无关
</code></pre><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">属性：</span></span><br><span class="line"><span class="string">    类属性：</span></span><br><span class="line"><span class="string">    实例属性：</span></span><br><span class="line"><span class="string">方法：</span></span><br><span class="line"><span class="string">    实例方法</span></span><br><span class="line"><span class="string">    类方法</span></span><br><span class="line"><span class="string">    静态方法</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Method_Attributes</span>:</span><br><span class="line">    <span class="comment"># 类属性</span></span><br><span class="line">    <span class="built_in">id</span> = <span class="number">1</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name</span>):</span><br><span class="line">        <span class="comment"># 实例属性</span></span><br><span class="line">        self._name = name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">name</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> self._name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @name.setter</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">name</span>(<span class="params">self, name</span>):</span><br><span class="line">        self._name = name</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 实例方法</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">say</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;我是实例方法&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 类方法</span></span><br><span class="line"><span class="meta">    @classmethod</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">class_method</span>(<span class="params">cls</span>):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;我是类方法&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 静态方法</span></span><br><span class="line"><span class="meta">    @staticmethod</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">static_method</span>():</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;我是静态方法&quot;</span>)</span><br><span class="line"></span><br><span class="line">method = Method_Attributes(<span class="string">&quot;李白&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;我是类属性：%s&quot;</span> % method.<span class="built_in">id</span>)  <span class="comment"># 我是类属性：1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;我是实例属性：%s&quot;</span> % method.name)  <span class="comment"># 我是实例属性：李白</span></span><br><span class="line">method.say()  <span class="comment"># 我是实例方法	</span></span><br><span class="line"></span><br><span class="line">Method_Attributes.class_method()  <span class="comment"># 我是类方法</span></span><br><span class="line">Method_Attributes.static_method()  <span class="comment"># 我是静态方法</span></span><br></pre></td></tr></table></figure>
<h2 id="11、垃圾回收"><a href="#11、垃圾回收" class="headerlink" title="11、垃圾回收"></a>11、垃圾回收</h2><ul>
<li>就像我们生活中会产生垃圾一样，程序在运行过程当中也会产生垃圾</li>
<li>程序运行过程中产生的垃圾会影响到程序的运行的运行性能，所以这些垃圾必须被及时清理</li>
<li>没用的东西就是垃圾</li>
<li>在程序中没有被引用的对象就是垃圾，这种垃圾对象过多以后会影响到程序的运行的性能</li>
<li>所以我们必须进行及时的垃圾回收，所谓的垃圾回收就是讲垃圾对象从内存中删除</li>
<li>在Python中有自动的垃圾回收机制，它会自动将这些没有被引用的对象删除，</li>
<li>所以我们不用手动处理垃圾回收</li>
<li>当垃圾回收的时候会自动执行—del—方法，del是一个特殊方法，它会在对象被垃圾回收前调用</li>
</ul>
<h2 id="12、特殊方法"><a href="#12、特殊方法" class="headerlink" title="12、特殊方法"></a>12、特殊方法</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># object.__add__(self, other)</span></span><br><span class="line"><span class="comment"># object.__sub__(self, other)</span></span><br><span class="line"><span class="comment"># object.__mul__(self, other)</span></span><br><span class="line"><span class="comment"># object.__matmul__(self, other)</span></span><br><span class="line"><span class="comment"># object.__truediv__(self, other)</span></span><br><span class="line"><span class="comment"># object.__floordiv__(self, other)</span></span><br><span class="line"><span class="comment"># object.__mod__(self, other)</span></span><br><span class="line"><span class="comment"># object.__divmod__(self, other)</span></span><br><span class="line"><span class="comment"># object.__pow__(self, other[, modulo])</span></span><br><span class="line"><span class="comment"># object.__lshift__(self, other)</span></span><br><span class="line"><span class="comment"># object.__rshift__(self, other)</span></span><br><span class="line"><span class="comment"># object.__and__(self, other)</span></span><br><span class="line"><span class="comment"># object.__xor__(self, other)</span></span><br><span class="line"><span class="comment"># object.__or__(self, other)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># object.__lt__(self, other) 小于 &lt;</span></span><br><span class="line"><span class="comment"># object.__le__(self, other) 小于等于 &lt;=</span></span><br><span class="line"><span class="comment"># object.__eq__(self, other) 等于 ==</span></span><br><span class="line"><span class="comment"># object.__ne__(self, other) 不等于 !=</span></span><br><span class="line"><span class="comment"># object.__gt__(self, other) 大于 &gt;</span></span><br><span class="line"><span class="comment"># object.__ge__(self, other) 大于等于 &gt;=</span></span><br></pre></td></tr></table></figure>
<ul>
<li><p>特殊方法，也称为魔术方法</p>
</li>
<li><p>特殊方法都是使用__开头和结尾的</p>
</li>
<li><p>特殊方法一般不需要我们手动调用，需要在一些特殊情况下自动执行</p>
<ul>
<li><strong> gt</strong>会在对象做大于比较的时候调用，和Java中的比较器差不多，可以比较自定义类的大小</li>
<li>object.<strong> bool</strong>(self)，可以通过bool来指定对象转换为布尔值的情况</li>
<li><strong> str</strong>(self)：和Java中的toString()方法效果差不多</li>
<li><p><strong> repr</strong>()这个特殊方法会在对当前对象使用repr()函数时调用，它的作用是指定对象在 ‘交互模式’中直接输出的效果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># __len__()获取对象的长度</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, <span class="built_in">id</span>, name</span>):</span><br><span class="line">        self._name = name</span><br><span class="line">        self._<span class="built_in">id</span> = <span class="built_in">id</span></span><br><span class="line"></span><br><span class="line"><span class="meta">    @property</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">name</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> self._name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @name.setter</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">name</span>(<span class="params">self, name</span>):</span><br><span class="line">        self._name = name</span><br><span class="line"></span><br><span class="line">    <span class="comment"># __gt__会在对象做大于比较的时候调用，该方法的返回值将会作为比较的结果</span></span><br><span class="line">    <span class="comment"># 他需要两个参数，一个self表示当前对象，other表示和当前对象比较的对象</span></span><br><span class="line">    <span class="comment"># self &gt; other  :  &gt;左边的为self，&gt;右边的为other</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__gt__</span>(<span class="params">self, other</span>):</span><br><span class="line">        <span class="comment"># 该方法和Java中的比较器类似，可以比较自定义类的大小</span></span><br><span class="line">        <span class="keyword">return</span> self._<span class="built_in">id</span> &gt; other._<span class="built_in">id</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># object.__bool__(self)，可以通过bool来指定对象转换为布尔值的情况</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__bool__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="comment"># 当id&gt;18返回True，否则返回False</span></span><br><span class="line">        <span class="keyword">return</span> self._<span class="built_in">id</span> &gt; <span class="number">18</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 和Java中的toString()方法差不多</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__str__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;Person [name=%s , id=%d]&#x27;</span> % (self._name, self._<span class="built_in">id</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># __repr__()这个特殊方法会在对当前对象使用repr()函数时调用</span></span><br><span class="line">    <span class="comment"># 它的作用是指定对象在 ‘交互模式’中直接输出的效果</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__repr__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;Person [name=%s , id=%d]&#x27;</span> % (self._name, self._<span class="built_in">id</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>剩余方法的使用可以上网查询使用</p>
</li>
</ul>
<h2 id="13、模块"><a href="#13、模块" class="headerlink" title="13、模块"></a>13、模块</h2><p>模块（module）</p>
<ul>
<li>模块化，模块化指将一个完整的程序分解为一个一个小的模块，通过将模块组合，来搭建出一个完整的程序</li>
<li>模块化和不模块化的区别<ul>
<li>不采用模块化，统一将所有的代码编写到一个文件中</li>
<li>采用模块化，将程序分别编写到多个文件中</li>
</ul>
</li>
<li><p>模块化的有点：</p>
<ul>
<li>方便开发</li>
<li>方便维护</li>
<li>模块可以复用！</li>
</ul>
</li>
<li><p>在Python中一个py文件就是一个模块，要想创建模块，实际上就是创建一个python文件</p>
</li>
<li>注意：模块名要符号标识符的规范</li>
<li>在一个模块中引入外部模块<ul>
<li>import 模块名 （模块名，就是python文件的名字，注意不要py）</li>
<li>import 模块名 as 模块别名<ul>
<li>可以引入同一个模块多次，但是模块的实例只会创建一个，模块中代码只会执行一次</li>
<li>import可以在程序的任意位置调用，但是一般情况下，import语句都会统一写在程序的开头</li>
<li>在每一个模块内部都有一个<strong>name</strong>属性，通过这个属性可以获取到模块的名字</li>
<li><strong> name</strong> 属性值为 <strong> main</strong>的模块是主模块，一个程序中只会有一个主模块，主模块就是我们直接通过 python 执行的模块，谁在执行，谁就是主模块</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="14、模块的使用"><a href="#14、模块的使用" class="headerlink" title="14、模块的使用"></a>14、模块的使用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> xxx  <span class="comment"># 引入一个模块</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> xxx <span class="keyword">as</span> yyy  <span class="comment"># 引入一个模块并给模块起别名</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> xxx <span class="keyword">import</span> yyy , zzz , fff  <span class="comment"># 从模块中引入方法或属性，调用的时候直接写属性和方法名，不需要模块名.属性/方法名</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> xxx <span class="keyword">import</span> *  <span class="comment"># 导入模块中的所有东西，一般不会使用</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> xxx <span class="keyword">import</span> yyy <span class="keyword">as</span> zz  <span class="comment"># 导入模块中的属性或方法并且给属性/方法起别名</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 可以在模块中定义变量，在模块中定义的变量，在引入模块后，就可以直接使用了</span></span><br><span class="line">a = <span class="number">10</span></span><br><span class="line">b = <span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 添加了_的变量，只能在模块内部访问，在通过import * 引入时，不会引入_开头的变量</span></span><br><span class="line">_c = <span class="number">30</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 可以在模块中定义函数，同样可以通过模块访问到</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">test</span>():</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;test&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">test2</span>():</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;test2&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 也可以定义类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>:</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        self.name = <span class="string">&#x27;孙悟空&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 编写测试代码，这部分代码，只要当当前文件作为主模块的时候才需要执行</span></span><br><span class="line"><span class="comment"># 而当模块被其他模块引入时，不需要执行的，此时我们就必须要检查当前模块是否是主模块  </span></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    test()</span><br><span class="line">    test2()</span><br><span class="line">    p = Person()</span><br><span class="line">    <span class="built_in">print</span>(p.name)</span><br></pre></td></tr></table></figure>
<h2 id="15、包"><a href="#15、包" class="headerlink" title="15、包"></a>15、包</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#导入包下面的init方法中的变量和方法</span></span><br><span class="line"><span class="keyword">import</span> 包名</span><br><span class="line"><span class="comment"># 导入指定包下面的新建的python文件</span></span><br><span class="line"><span class="keyword">from</span> 包名 <span class="keyword">import</span> a,v,b.....</span><br></pre></td></tr></table></figure>
<ul>
<li><p>包 Package：包也是一个模块</p>
</li>
<li><p>当我们模块中代码过多时，或者一个模块需要被分解为多个模块时，这时就需要使用到包</p>
</li>
<li><p>普通的模块就是一个py文件，而包是一个文件夹</p>
</li>
<li><p>包中必须要一个一个<strong> init</strong>.py 这个文件，这个文件中可以包含有包中的主要内容</p>
</li>
</ul>
<h2 id="16、Python标准库"><a href="#16、Python标准库" class="headerlink" title="16、Python标准库"></a>16、Python标准库</h2><p>思想：开箱即用</p>
<ul>
<li>为了实现开箱即用的思想，Python中为我们提供了一个模块的标准库，在这个标准库中，有很多很强大的模块我们可以直接使用，并且标准库会随Python的安装一同安装</li>
</ul>
<ul>
<li><p>sys模块：提供了一些变量和函数，使我们可以获取到Python解析器的信息，或者通过函数来操控python解析器</p>
<ul>
<li><p>sys.argv:获取执行代码时，命令行中所包含的参数，</p>
<p>  返回的是一个列表，列表中保存了当前命令的所有参数</p>
</li>
<li><p>sys.module：获取当前执行文件引入的所有模块</p>
<p>  返回的是一个字典，字典的key是模块的名字，字典的value是模块的对象</p>
</li>
<li><p>sys.path：返回一个列表，列表中保存模块的搜索路径</p>
</li>
<li><p>sys.exit：函数用来退出程序</p>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>pprint模块：</p>
<ul>
<li>pprint()：对打印的数据输出进行简单的格式化输出</li>
</ul>
</li>
</ul>
<ul>
<li><p>os模块（operator）：可以让我们对系统进行访问</p>
<ul>
<li>os.environ：这个可以获取到系统的环境变量（os.environ[‘path’]）</li>
<li>os.system：可以用来执行操作系统的名字（可以和cmd一样执行命令行）</li>
</ul>
</li>
</ul>
<ul>
<li>具体的模块使用可以去标准库寻找使用方法</li>
</ul>
<h1 id="七、异常"><a href="#七、异常" class="headerlink" title="七、异常"></a>七、异常</h1><h2 id="1、异常"><a href="#1、异常" class="headerlink" title="1、异常"></a>1、异常</h2><ul>
<li>程序在运行过程当中，不可避免的会出现一些错误，比如：<ul>
<li>使用了没有赋值过的变量</li>
<li>使用了不存在的索引</li>
<li>除0</li>
<li>​    …</li>
</ul>
</li>
<li>这些错误在程序中，我们称其为异常。</li>
<li>程序运行过程中，一旦出现异常将会导致程序立即终止，异常以后的代码全部都不会执行！</li>
</ul>
<h2 id="2、处理异常"><a href="#2、处理异常" class="headerlink" title="2、处理异常"></a>2、处理异常</h2><ul>
<li><p>程序运行时出现异常，目的并不是让我们的程序直接终止！</p>
</li>
<li><p>Python是希望在出现异常时，我们可以编写代码来对异常进行处理！    </p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>语句</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        代码块（可能出现错误的语句）</span><br><span class="line">    <span class="keyword">except</span> 异常类型 <span class="keyword">as</span> 异常名:</span><br><span class="line">        代码块（出现错误以后的处理方式）</span><br><span class="line">    <span class="keyword">except</span> 异常类型 <span class="keyword">as</span> 异常名:</span><br><span class="line">        代码块（出现错误以后的处理方式）</span><br><span class="line">    <span class="keyword">except</span> 异常类型 <span class="keyword">as</span> 异常名:</span><br><span class="line">        代码块（出现错误以后的处理方式）</span><br><span class="line">    <span class="keyword">else</span>：</span><br><span class="line">        代码块（没出错时要执行的语句）    </span><br><span class="line">    <span class="keyword">finally</span>:</span><br><span class="line">        代码块（该代码块总会执行）</span><br><span class="line">        </span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">try是必须的 else语句有没有都行</span></span><br><span class="line"><span class="string">except和finally至少有一个    </span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>可以将可能出错的代码放入到try语句，这样如果代码没有错误，则会正常执行，如果出现错误，则会执行expect子句中的代码，这样我们就可以通过代码来处理异常避免因为一个异常导致整个程序的终止            </p>
</li>
</ul>
<h2 id="2、异常传播"><a href="#2、异常传播" class="headerlink" title="2、异常传播"></a>2、异常传播</h2><ul>
<li><p>当在函数中出现异常时，如果在函数中对异常进行了处理，则异常不会再继续传播，</p>
</li>
<li><p>如果函数中没有对异常进行处理，则异常会继续向函数调用处传播</p>
</li>
<li><p>如果函数调用处处理了异常，则不再传播</p>
</li>
<li><p>如果没有处理则继续向调用处传播， 直到传递到全局作用域（主模块）如果依然没有处理，则程序终止，并且显示异常信息</p>
</li>
</ul>
<ul>
<li><p>当程序运行过程中出现异常以后，所有的异常信息会被保存一个专门的异常对象中</p>
</li>
<li><p>而异常传播时，实际上就是异常对象抛给了调用处</p>
<ul>
<li>比如 ： <ul>
<li>ZeroDivisionError类的对象专门用来表示除0的异常</li>
<li>NameError类的对象专门用来处理变量错误的异常</li>
<li>….</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul>
<li>在Python为我们提供了多个异常对象       </li>
</ul>
<h2 id="3、抛出异常和自定义异常"><a href="#3、抛出异常和自定义异常" class="headerlink" title="3、抛出异常和自定义异常"></a>3、抛出异常和自定义异常</h2><ul>
<li><p>可以使用 raise 语句来抛出异常：raise语句后需要跟一个异常类 或 异常的实例</p>
</li>
<li><p>也可以自定义异常类，只需要创建一个类继承Exception即可</p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 也可以自定义异常类，只需要创建一个类继承Exception即可</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyError</span>(<span class="title class_ inherited__">Exception</span>):</span><br><span class="line">    <span class="keyword">pass</span>    </span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">a, b</span>):</span><br><span class="line">    <span class="comment"># 如果a和b中有负数，就向调用处抛出异常</span></span><br><span class="line">    <span class="keyword">if</span> a &lt; <span class="number">0</span> <span class="keyword">or</span> b &lt; <span class="number">0</span>:</span><br><span class="line">        <span class="comment"># raise用于向外部抛出异常，后边可以跟一个异常类，或异常类的实例</span></span><br><span class="line">        <span class="comment"># raise Exception</span></span><br><span class="line">        <span class="comment"># 抛出异常的目的，告诉调用者这里调用时出现问题，希望你自己处理一下</span></span><br><span class="line">        <span class="comment"># raise Exception(&#x27;两个参数中不能有负数！&#x27;)</span></span><br><span class="line">        <span class="keyword">raise</span> MyError(<span class="string">&#x27;自定义的异常&#x27;</span>)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 也可以通过if else来代替异常的处理</span></span><br><span class="line">        <span class="comment"># return None</span></span><br><span class="line">    r = a + b</span><br><span class="line">    <span class="keyword">return</span> r</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(add(<span class="number">123</span>, <span class="number">456</span>))</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h1 id="八、文件"><a href="#八、文件" class="headerlink" title="八、文件"></a>八、文件</h1><ul>
<li>通过Python程序来对计算机中的各种文件进行增删改查的操作</li>
<li>I/O(Input / Output)</li>
<li>操作文件的步骤：<ul>
<li>打开文件</li>
<li>对文件进行各种操作（读、写），然后保存</li>
<li>关闭文件</li>
</ul>
</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>Python基础</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="https://www.naste.top:1024/posts/99832f47.html">https://www.naste.top:1024/posts/99832f47.html</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>XiaoFei🥝</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2022-06-21</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2024-04-26</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Python/"><div class="tags-punctuation"><svg class="faa-tada icon" style="height:1.1em;width:1.1em;fill:currentColor;position:relative;top:2px;margin-right:3px" aria-hidden="true"><use xlink:href="#icon-sekuaibiaoqian"></use></svg></div>Python</a></div></div><link rel="stylesheet" href="/css/coin.css" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">投喂作者</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://www.naste.top:9000/images/wechat.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="https://www.naste.top:9000/images/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://www.naste.top:9000/images/alipay.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="https://www.naste.top:9000/images/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><audio id="coinAudio" src="https://npm.elemecdn.com/akilar-candyassets@1.0.36/audio/aowu.m4a"></audio><script defer="defer" src="/js/coin.js"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/ad44f2a3.html"><img class="prev-cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/2514ce637e374a318653a40133bc66c5.webp" onerror="onerror=null;src='/assets/r2.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Python爬虫基本使用</div></div></a></div><div class="next-post pull-right"><a href="/posts/bae4ff13.html"><img class="next-cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1515952743789.webp" onerror="onerror=null;src='/assets/r2.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Redis</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/ad44f2a3.html" title="Python爬虫基本使用"><img class="cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/2514ce637e374a318653a40133bc66c5.webp" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-04-26</div><div class="title">Python爬虫基本使用</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><svg class="meta_icon" style="width:22px;height:22px;position:relative;top:5px"><use xlink:href="#icon-mulu1"></use></svg><span style="font-weight:bold">目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81Python%E5%85%A5%E9%97%A8"><span class="toc-text">一、Python入门</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81Python%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-text">1、Python基本概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-text">2、基本语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E5%AD%97%E9%9D%A2%E9%87%8F%E5%92%8C%E5%8F%98%E9%87%8F"><span class="toc-text">3、字面量和变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E5%8F%98%E9%87%8F%E5%92%8C%E6%A0%87%E8%AF%86%E7%AC%A6"><span class="toc-text">4、变量和标识符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">5、数据类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2-str"><span class="toc-text">6、字符串(str)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E6%A0%BC%E5%BC%8F%E5%8C%96%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">7、格式化字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%A4%8D%E5%88%B6"><span class="toc-text">8、字符串复制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9%E3%80%81%E5%B8%83%E5%B0%94%E5%80%BC%E5%92%8C%E7%A9%BA%E5%80%BC"><span class="toc-text">9、布尔值和空值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10%E3%80%81%E7%B1%BB%E5%9E%8B%E6%A3%80%E6%9F%A5"><span class="toc-text">10、类型检查</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11%E3%80%81%E5%AF%B9%E8%B1%A1"><span class="toc-text">11、对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12%E3%80%81%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%BB%93%E6%9E%84"><span class="toc-text">12、对象的结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13%E3%80%81%E5%8F%98%E9%87%8F%E5%92%8C%E5%AF%B9%E8%B1%A1"><span class="toc-text">13、变量和对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14%E3%80%81%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">14、类型转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15%E3%80%81%E8%BF%90%E7%AE%97%E7%AC%A6-%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="toc-text">15、运算符(操作符)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#16%E3%80%81%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">16、赋值运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#17%E3%80%81%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">17、关系运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#18%E3%80%81%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">18、逻辑运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#19%E3%80%81%E6%9D%A1%E4%BB%B6%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%88%E4%B8%89%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6%EF%BC%89"><span class="toc-text">19、条件运算符（三元运算符）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#20%E3%80%81%E8%BF%90%E7%AE%97%E7%AC%A6%E7%9A%84%E4%BC%98%E5%85%88%E7%BA%A7"><span class="toc-text">20、运算符的优先级</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="toc-text">二、流程控制语句</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E8%AF%AD%E5%8F%A5"><span class="toc-text">1、条件判断语句</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81input%E5%87%BD%E6%95%B0"><span class="toc-text">2、input函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81if-else"><span class="toc-text">3、if-else</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81if-elif-else"><span class="toc-text">4、if-elif-else</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81if-%EF%BC%88if-else%EF%BC%89-else"><span class="toc-text">5、if-（if-else）-else</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="toc-text">三、循环语句</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81while%E5%BE%AA%E7%8E%AF"><span class="toc-text">1、while循环</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E5%BE%AA%E7%8E%AF%E5%B5%8C%E5%A5%97"><span class="toc-text">2、循环嵌套</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81break%E3%80%81continue%E5%92%8Cpass"><span class="toc-text">3、break、continue和pass</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E6%A8%A1%E5%9D%97"><span class="toc-text">4、模块</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%BA%8F%E5%88%97"><span class="toc-text">四、序列</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E5%88%97%E8%A1%A8%EF%BC%88list%EF%BC%89%E7%9A%84%E7%9A%84%E7%AE%80%E4%BB%8B"><span class="toc-text">1、列表（list）的的简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E5%88%87%E7%89%87"><span class="toc-text">2、切片</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E5%BA%8F%E5%88%97"><span class="toc-text">4、序列</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E9%80%9A%E8%BF%87%E7%B4%A2%E5%BC%95%E5%92%8C%E5%88%87%E7%89%87%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0"><span class="toc-text">5、通过索引和切片修改元素</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E9%80%9A%E8%BF%87%E6%96%B9%E6%B3%95%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0"><span class="toc-text">6、通过方法修改元素</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81for%E5%BE%AA%E7%8E%AF"><span class="toc-text">8、for循环</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E9%81%8D%E5%8E%86%E5%88%97%E8%A1%A8"><span class="toc-text">7、遍历列表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81range%EF%BC%88%EF%BC%89%E5%87%BD%E6%95%B0"><span class="toc-text">8、range（）函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81%E5%85%83%E7%BB%84-%E4%B8%8D%E5%8F%AF%E5%8F%98%E5%BA%8F%E5%88%97"><span class="toc-text">8、元组(不可变序列)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9%E3%80%81%E5%8F%AF%E5%8F%98%E5%AF%B9%E8%B1%A1"><span class="toc-text">9、可变对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10%E3%80%81python%E4%B8%AD%E7%9A%84-%E3%80%81-%E3%80%81is%E3%80%81is-not"><span class="toc-text">10、python中的&#x3D;&#x3D;、!&#x3D;、is、is not</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11%E3%80%81%E5%AD%97%E5%85%B8%EF%BC%88dict%EF%BC%89"><span class="toc-text">11、字典（dict）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12%E3%80%81%E5%AD%97%E5%85%B8%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">12、字典的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13%E3%80%81%E5%AD%97%E5%85%B8%E7%9A%84%E9%81%8D%E5%8E%86"><span class="toc-text">13、字典的遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14%E3%80%81%E9%9B%86%E5%90%88%EF%BC%88set%EF%BC%89"><span class="toc-text">14、集合（set）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15%E3%80%81%E5%8F%AF%E5%8F%98%E5%AF%B9%E8%B1%A1%E5%92%8C%E4%B8%8D%E5%8F%AF%E5%8F%98%E5%AF%B9%E8%B1%A1%E5%88%86%E5%88%AB%E6%9C%89%E5%93%AA%E4%BA%9B"><span class="toc-text">15、可变对象和不可变对象分别有哪些</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%87%BD%E6%95%B0"><span class="toc-text">五、函数</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E5%87%BD%E6%95%B0%E7%AE%80%E4%BB%8B"><span class="toc-text">1、函数简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E5%88%9B%E5%BB%BA%E5%87%BD%E6%95%B0"><span class="toc-text">2、创建函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E5%87%BD%E6%95%B0%E7%9A%84%E5%8F%82%E6%95%B0"><span class="toc-text">3、函数的参数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E4%B8%8D%E5%AE%9A%E9%95%BF%E5%8F%82%E6%95%B0%EF%BC%88%E5%92%8CJava%E4%B8%AD%E7%9A%84%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%E5%B7%AE%E4%B8%8D%E5%A4%9A%EF%BC%89"><span class="toc-text">4、不定长参数（和Java中的可变参数差不多）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-text">5、返回值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E6%96%87%E6%A1%A3%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">6、文档字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E4%BD%9C%E7%94%A8%E5%9F%9F%E5%92%8C%E5%91%BD%E5%90%8D%E7%A9%BA%E9%97%B4"><span class="toc-text">7、作用域和命名空间</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81%E9%80%92%E5%BD%92"><span class="toc-text">8、递归</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9%E3%80%81%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B"><span class="toc-text">9、函数式编程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10%E3%80%81%E9%AB%98%E9%98%B6%E5%87%BD%E6%95%B0"><span class="toc-text">10、高阶函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11%E3%80%81%E9%97%AD%E5%8C%85%E5%87%BD%E6%95%B0"><span class="toc-text">11、闭包函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12%E3%80%81%E8%A3%85%E9%A5%B0%E5%99%A8%EF%BC%88%E7%94%9F%E6%88%90%E4%BB%A3%E7%90%86%E7%B1%BB%EF%BC%89"><span class="toc-text">12、装饰器（生成代理类）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E4%BB%A3%E7%90%86"><span class="toc-text">静态代理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86"><span class="toc-text">动态代理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E6%B3%A8%E8%A7%A3"><span class="toc-text">使用注解</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"><span class="toc-text">六、面向对象</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E4%BB%80%E4%B9%88%E6%98%AF%E5%AF%B9%E8%B1%A1"><span class="toc-text">1、什么是对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1"><span class="toc-text">2、面向对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E7%B1%BB"><span class="toc-text">3、类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%B5%81%E7%A8%8B"><span class="toc-text">类创建对象的流程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-text">类的定义</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E5%AF%B9%E8%B1%A1%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">4、对象初始化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%811-5%E6%80%BB%E7%BB%93"><span class="toc-text">5、1-5总结</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E5%B0%81%E8%A3%85"><span class="toc-text">6、封装</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%81%E8%A3%851"><span class="toc-text">封装1</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%81%E8%A3%852"><span class="toc-text">封装2</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%81%E8%A3%853"><span class="toc-text">封装3</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E7%BB%A7%E6%89%BF"><span class="toc-text">7、继承</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%87%8D%E5%86%99"><span class="toc-text">重写</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81%E5%A4%9A%E9%87%8D%E7%BB%A7%E6%89%BF-%E4%BA%86%E8%A7%A3%E4%B8%8B"><span class="toc-text">8、多重继承(了解下)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9%E3%80%81%E5%A4%9A%E6%80%81"><span class="toc-text">9、多态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10%E3%80%81%E7%B1%BB%E4%B8%AD%E7%9A%84%E5%B1%9E%E6%80%A7%E5%92%8C%E6%96%B9%E6%B3%95"><span class="toc-text">10、类中的属性和方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11%E3%80%81%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6"><span class="toc-text">11、垃圾回收</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12%E3%80%81%E7%89%B9%E6%AE%8A%E6%96%B9%E6%B3%95"><span class="toc-text">12、特殊方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13%E3%80%81%E6%A8%A1%E5%9D%97"><span class="toc-text">13、模块</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14%E3%80%81%E6%A8%A1%E5%9D%97%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">14、模块的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15%E3%80%81%E5%8C%85"><span class="toc-text">15、包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#16%E3%80%81Python%E6%A0%87%E5%87%86%E5%BA%93"><span class="toc-text">16、Python标准库</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E5%BC%82%E5%B8%B8"><span class="toc-text">七、异常</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E5%BC%82%E5%B8%B8"><span class="toc-text">1、异常</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E5%A4%84%E7%90%86%E5%BC%82%E5%B8%B8"><span class="toc-text">2、处理异常</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E5%BC%82%E5%B8%B8%E4%BC%A0%E6%92%AD"><span class="toc-text">2、异常传播</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8%E5%92%8C%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8"><span class="toc-text">3、抛出异常和自定义异常</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E6%96%87%E4%BB%B6"><span class="toc-text">八、文件</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-color: transparent;"><div id="footer-wrap"><div id="ft"><div class="ft-item-1"><div class="t-top"><div class="t-t-l"><p class="ft-t t-l-t">格言🧬</p><div class="bg-ad"><div>再看看那个光点，它就在这里，这是家园，这是我们 —— 你所爱的每一个人，你认识的一个人，你听说过的每一个人，曾经有过的每一个人，都在它上面度过他们的一生✨</div><div class="btn-xz-box"><a class="btn-xz" target="_blank" rel="noopener" href="https://stellarium.org/">点击开启星辰之旅</a></div></div></div><div class="t-t-r"><p class="ft-t t-l-t">猜你想看💡</p><ul class="ft-links"><li><a href="/posts/eec9786.html">魔改指南</a><a href="/box/nav/">网址导航</a></li><li><a href="/social/link/">我的朋友</a><a href="/comments/">留点什么</a></li><li><a href="/personal/about/">关于作者</a><a href="/archives/">文章归档</a></li><li><a href="/categories/">文章分类</a><a href="/tags/">文章标签</a></li><li><a href="/box/Gallery/">我的画廊</a><a href="/personal/bb/">我的唠叨</a></li><li><a href="/site/time/">建设进程</a><a href="/site/census/">网站统计</a></li></ul></div></div></div><div class="ft-item-2"><p class="ft-t">推荐友链⌛</p><div class="ft-img-group"><div class="img-group-item"><a href="https://www.naste.top:1024/" title="XiaoFei🥝"><img src= "" data-lazy-src="https://www.naste.top:9000/images/cat.jpg" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div></div></div></div><div class="copyright"><span><b>&copy;2022-2024</b></span><span><b>&nbsp;&nbsp;By XiaoFei🥝</b></span></div><div id="workboard"></div><p id="ghbdages"><a class="github-badge" target="_blank" href="https://hexo.io/" style="margin-inline:5px" title="博客框架为Hexo_v6.3.0"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Frame-Hexo-blue.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://butterfly.js.org/" style="margin-inline:5px" title="主题版本Butterfly_v4.3.1"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Theme-Butterfly-6513df.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://vercel.com/" style="margin-inline:5px" title="本站采用多线部署，主线路托管于Vercel"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Hosted-Vercel-brightgreen.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://user.51.la/" style="margin-inline:5px" title="本站数据分析得益于51la技术支持"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Analytics-51la-3db1eb.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://github.com/" style="margin-inline:5px" title="本网站源码由Github提供存储仓库"><img src= "" data-lazy-src=" https://static.naste.top:1024/svg/blog/Source-Github-d021d6.svg" alt=""/></a></p></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><a class="icon-V hidden" onclick="switchNightMode()" title="浅色和深色模式转换"><svg width="25" height="25" viewBox="0 0 1024 1024"><use id="modeicon" xlink:href="#icon-moon"></use></svg></a><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button class="share" type="button" title="右键模式" onclick="changeMouseMode()"><i class="fas fa-mouse"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog right_side"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button class="share" type="button" title="分享链接" onclick="share()"><i class="fas fa-share-nodes"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i><span id="percent">0<span>%</span></span></button><button id="go-down" type="button" title="直达底部" onclick="btf.scrollToDest(document.body.scrollHeight, 500)"><i class="fas fa-arrow-down"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div class="js-pjax" id="rightMenu"><div class="rightMenu-group rightMenu-small"><a class="rightMenu-item" href="javascript:window.history.back();"><i class="fa fa-arrow-left"></i></a><a class="rightMenu-item" href="javascript:window.history.forward();"><i class="fa fa-arrow-right"></i></a><a class="rightMenu-item" href="javascript:window.location.reload();"><i class="fa fa-refresh"></i></a><a class="rightMenu-item" href="javascript:rmf.scrollToTop();"><i class="fa fa-arrow-up"></i></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-text"><a class="rightMenu-item" href="javascript:rmf.copySelect();"><i class="fa fa-copy"></i><span>复制</span></a><a class="rightMenu-item" href="javascript:window.open(&quot;https://www.baidu.com/s?wd=&quot;+window.getSelection().toString());window.location.reload();"><i class="fa fa-search"></i><span>百度搜索</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-too"><a class="rightMenu-item" href="javascript:window.open(window.getSelection().toString());window.location.reload();"><i class="fa fa-link"></i><span>转到链接</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-paste"><a class="rightMenu-item" href="javascript:rmf.paste()"><i class="fa fa-copy"></i><span>粘贴</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-post"><a class="rightMenu-item" href="#post-comment"><i class="fas fa-comment"></i><span>空降评论</span></a><a class="rightMenu-item" href="javascript:rmf.copyWordsLink()"><i class="fa fa-link"></i><span>复制本文地址</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-to"><a class="rightMenu-item" href="javascript:rmf.openWithNewTab()"><i class="fa fa-window-restore"></i><span>新窗口打开</span></a><a class="rightMenu-item" id="menu-too" href="javascript:rmf.open()"><i class="fa fa-link"></i><span>转到链接</span></a><a class="rightMenu-item" href="javascript:rmf.copyLink()"><i class="fa fa-copy"></i><span>复制链接</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-img"><a class="rightMenu-item" href="javascript:rmf.saveAs()"><i class="fa fa-download"></i><span>保存图片</span></a><a class="rightMenu-item" href="javascript:rmf.openWithNewTab()"><i class="fa fa-window-restore"></i><span>在新窗口打开</span></a><a class="rightMenu-item" href="javascript:rmf.copyLink()"><i class="fa fa-copy"></i><span>复制图片链接</span></a></div><div class="rightMenu-group rightMenu-line"><a class="rightMenu-item" href="javascript:randomPost()"><i class="fa fa-paper-plane"></i><span>随便逛逛</span></a><a class="rightMenu-item" href="javascript:switchNightMode();"><i class="fa fa-moon"></i><span>昼夜切换</span></a><a class="rightMenu-item" href="/personal/about/"><i class="fa fa-info-circle"></i><span>关于博客</span></a><a class="rightMenu-item" href="javascript:toggleWinbox();"><i class="fas fa-cog"></i><span>美化设置</span></a><a class="rightMenu-item" href="javascript:rmf.fullScreen();"><i class="fas fa-expand"></i><span>切换全屏</span></a><a class="rightMenu-item" href="javascript:window.print();"><i class="fa-solid fa-print"></i><span>打印页面</span></a></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.umd.min.js"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/instant.page/5.1.0/instantpage.min.js" type="module"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vanilla-lazyload/17.3.1/lazyload.iife.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (true){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><script async="async">var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())
setTimeout(function(){preloader.endLoading();}, 5000);
document.getElementById('loading-box').addEventListener('click',()=> {preloader.endLoading()})</script><div class="js-pjax"><script>(()=>{
  const init = () => {
    twikoo.init(Object.assign({
      el: '#twikoo-wrap',
      envId: '',
      region: '',
      onCommentLoaded: function () {
        btf.loadLightbox(document.querySelectorAll('#twikoo .tk-content img:not(.tk-owo-emotion)'))
      }
    }, null))
  }

  const getCount = () => {
    const countELement = document.getElementById('twikoo-count')
    if(!countELement) return
    twikoo.getCommentsCount({
      envId: '',
      region: '',
      urls: [window.location.pathname],
      includeReply: false
    }).then(function (res) {
      countELement.innerText = res[0].count
    }).catch(function (err) {
      console.error(err);
    });
  }

  const runFn = () => {
    init()
    
  }

  const loadTwikoo = () => {
    if (typeof twikoo === 'object') {
      setTimeout(runFn,0)
      return
    } 
    getScript('https://cdn.staticfile.org/twikoo/1.6.8/twikoo.all.min.js').then(runFn)
  }

  if ('Twikoo' === 'Twikoo' || !true) {
    if (true) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
    else loadTwikoo()
  } else {
    window.loadOtherComment = () => {
      loadTwikoo()
    }
  }
})()</script></div><script src="https://cdn.staticfile.org/jquery/3.6.3/jquery.min.js"></script><script async src="https://cdn1.tianli0.top/npm/vue@2.6.14/dist/vue.min.js"></script><script async src="https://cdn1.tianli0.top/npm/element-ui@2.15.6/lib/index.js"></script><script async src="https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script><script defer type="text/javascript" src="https://cdn1.tianli0.top/npm/sweetalert2@8.19.0/dist/sweetalert2.all.js"></script><script async src="//npm.elemecdn.com/pace-js@1.2.4/pace.min.js"></script><script defer src="https://cdn1.tianli0.top/gh/nextapps-de/winbox/dist/winbox.bundle.min.js"></script><script async src="//at.alicdn.com/t/c/font_3586335_hsivh70x0fm.js"></script><script async src="//at.alicdn.com/t/c/font_3636804_gr02jmjr3y9.js"></script><script async src="//at.alicdn.com/t/c/font_3612150_kfv55xn3u2g.js"></script><script async src="https://cdn.wpon.cn/2022-sucai/Gold-ingot.js"></script><canvas id="universe"></canvas><canvas id="snow"></canvas><script defer src="/js/fomal.js"></script><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/aplayer/1.10.1/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/aplayer/1.10.1/APlayer.min.js"></script><script src="https://cdn1.tianli0.top/npm/js-heo@1.0.12/metingjs/Meting.min.js"></script><script src="https://lib.baomitu.com/pjax/0.2.8/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show","#web_bg",".js-pjax","#bibi","body > title","#app","#tag-echarts","#posts-echart","#categories-echarts"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --> <script data-pjax>if(document.getElementById('recent-posts') && (location.pathname ==='/'|| '/' ==='all')){
    var parent = document.getElementById('recent-posts');
    var child = '<div class="recent-post-item" style="width:100%;height: auto"><div id="catalog_magnet"><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Java/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍡 XiaoFeiのJava学习笔记 (55)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Linux/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍼 XiaoFeiのLinux笔记 (33)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Python/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍉 XiaoFeiのPython笔记 (2)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/前端/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍟 XiaoFeiの前端笔记 (17)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Other/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍥 XiaoFeiのOther笔记 (10)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item" style="visibility: hidden"></div><a class="magnet_link_more"  href="https://www.naste.top:1024/categories/" style="flex:1;text-align: center;margin-bottom: 10px;">查看更多...</a></div></div>';
    console.log('已挂载magnet')
    parent.insertAdjacentHTML("afterbegin",child)}
     </script><style>#catalog_magnet{flex-wrap: wrap;display: flex;width:100%;justify-content:space-between;padding: 10px 10px 0 10px;align-content: flex-start;}.magnet_item{flex-basis: calc(33.333333333333336% - 5px);background: #e9e9e9;margin-bottom: 10px;border-radius: 8px;transition: all 0.2s ease-in-out;}.magnet_item:hover{background: var(--text-bg-hover)}.magnet_link_more{color:#555}.magnet_link{color:black}.magnet_link:hover{color:white}@media screen and (max-width: 600px) {.magnet_item {flex-basis: 100%;}}.magnet_link_context{display:flex;padding: 10px;font-size:16px;transition: all 0.2s ease-in-out;}.magnet_link_context:hover{padding: 10px 20px;}</style>
    <style></style><script data-pjax>
  function butterfly_swiper_injector_config(){
    var parent_div_git = document.getElementById('recent-posts');
    var item_html = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/204253054.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://www.naste.top:9000/webp/1675912191.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-08</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/204253054.html&quot;);" href="javascript:void(0);" alt="">SpringBoot使用实现定时任务和xxl-job实现分布式任务调度</a><div class="blog-slider__text">SpringBoot使用实现定时任务和xxl-job实现分布式任务调度</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/204253054.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/538133837.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://www.naste.top:9000/webp/1678672933.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-15</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/538133837.html&quot;);" href="javascript:void(0);" alt="">Hexo中Markdown语法与外挂标签写法汇总</a><div class="blog-slider__text">Hexo中Markdown语法与外挂标签写法汇总</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/538133837.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
    console.log('已挂载butterfly_swiper')
    parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  var elist = 'undefined'.split(',');
  var cpage = location.pathname;
  var epage = '/';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_swiper_injector_config();
  }
  else if (epage === cpage){
    butterfly_swiper_injector_config();
  }
  </script><script defer src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper_init.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '2s');
    arr[i].setAttribute('data-wow-delay', '200ms');
    arr[i].setAttribute('data-wow-offset', '30');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '2s');
    arr[i].setAttribute('data-wow-delay', '200ms');
    arr[i].setAttribute('data-wow-offset', '30');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script></div><script defer src="https://npm.elemecdn.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://npm.elemecdn.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><script data-pjax src="https://npm.elemecdn.com/hexo-filter-gitcalendar/lib/gitcalendar.js"></script><script data-pjax>
  function gitcalendar_injector_config(){
      var parent_div_git = document.getElementById('gitZone');
      var item_html = '<div class="recent-post-item" id="gitcalendarBar" style="width:100%;height:auto;padding:10px;"><style>#git_container{min-height: 320px}@media screen and (max-width:650px) {#git_container{min-height: 0px}}</style><div id="git_loading" style="width:10%;height:100%;margin:0 auto;display: block;"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 50 50" style="enable-background:new 0 0 50 50" xml:space="preserve"><path fill="#d0d0d0" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z" transform="rotate(275.098 25 25)"><animatetransform attributeType="xml" attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="0.6s" repeatCount="indefinite"></animatetransform></path></svg><style>#git_container{display: none;}</style></div><div id="git_container"></div></div>';
      parent_div_git.insertAdjacentHTML("afterbegin",item_html)
      console.log('已挂载gitcalendar')
      }

    if( document.getElementById('gitZone') && (location.pathname ==='/personal/about/'|| '/personal/about/' ==='all')){
        gitcalendar_injector_config()
        GitCalendarInit("https://gitcalendar.naste.top/api?xiao-i-fei",['#d9e0df', '#c6e0dc', '#a8dcd4', '#9adcd2', '#89ded1', '#77e0d0', '#5fdecb', '#47dcc6', '#39dcc3', '#1fdabe', '#00dab9'],'xiao-i-fei')
    }
  </script><!-- hexo injector body_end end --></body></html>